1

hey As part of a project ive had to write a small IRC client to embed in it. Everything works nicely until the program is closed and the thread that reads input from the IRC channel is waiting for more input and never dies.

while(((inBuffer=in.readLine())!=null)&&(die==false))

inBuffer is simply a string and in is a buffered reader on the socket. the die variable is a boolean and my thought was that i can set that to true and it will fall out the thread. The problem is that "inBuffer=in.readLine()" is sitting there until it gets another line.
Can someone give me a hand?

Alex
  • 650
  • 9
  • 23

4 Answers4

4

Close the in stream, readLine() should then return null instantly.

thejh
  • 44,854
  • 16
  • 96
  • 107
  • i tried making a method called die() that simply did in.close() but then i had to make that method declare that it throws IOException and put the place where i call imReader.die() inside a try/catch block which simply doesnt work. – Alex Dec 15 '10 at 18:31
  • @Alex: Try to put a try-catch-clause around the while loop. – thejh Dec 15 '10 at 18:46
0

You could set a timeout on the socket and if you get a timeout exception, you can poll a abort-flag. If it's unset, try again to read.

Mot
  • 28,248
  • 23
  • 84
  • 121
0

Shutdown the socket for input. That will deliver a null to the readLine() invocation and terminate the loop.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

forgot bufferedReader had a "ready()" method to test if it has anything in the buffer. testing this did the trick.

Alex
  • 650
  • 9
  • 23
  • You got lucky. It only tests whether there was one character. If there *is* only one character, or a partial line without a line terminator, `readLine()` will still block waiting for it. – user207421 Mar 03 '19 at 02:56