How can I interrupt a ServerSocket accept() method?

Posted by lukeo05 on Stack Overflow See other posts from Stack Overflow or by lukeo05
Published on 2010-06-06T10:21:34Z Indexed on 2010/06/06 10:32 UTC
Read the original article Hit count: 329

Filed under:
|
|
|

Hi,

In my main thread I have a while(listening) loop which calls accept() on my ServerSocket object, then starts a new client thread and adds it to a Collection when a new client is accepted.

I also have an Admin thread which I want to use to issue commands, like 'exit', which will cause all the client threads to be shut down, shut itself down, and shut down the main thread, by turning listening to false.

However, the accept() call in the while(listening) loop blocks, and there doesn't seem to be any way to interrupt it, so the while condition cannot be checked again and the program cannot exit!

Is there a better way to do this? Or some way to interrupt the blocking method?

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about network