Alex
Alex

Reputation: 660

Kill a thread that is waiting for socket output

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?

Upvotes: 1

Views: 1228

Answers (4)

user207421
user207421

Reputation: 311050

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

Upvotes: 0

Mot
Mot

Reputation: 29600

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.

Upvotes: 0

Alex
Alex

Reputation: 660

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

Upvotes: -1

thejh
thejh

Reputation: 45578

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

Upvotes: 4

Related Questions