aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes How to interrupt a thread waiting for client connection? (URGENT) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "How to interrupt a thread waiting for client connection? (URGENT)" Watch "How to interrupt a thread waiting for client connection? (URGENT)" New topic
Author

How to interrupt a thread waiting for client connection? (URGENT)

sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Hi,
The main thread of server sets the status to IDLE and then it starts another thread (child thread). Now the main thread is waiting for client connection (by calling Socket.accept()method ).
In the mean time, the child thread sets the server status to STOPPING due to the request it received.
Now the server main thread should not continue waiting for the client connection if the status had been set to STOPPING.
Is there any way to make the server thread to come out of the blocking state (i.e, it blocks until it accepts a connection and it should come out of it).
Is there any way for the server thread to check for the status while it is blocking for a client connection?
Thanks in advance...
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Use ServerSocket.setSoTimeout to either periodically drop out of accept() to check your flag, or to simply set the maximum amount of time you're prepared to wait.
Alternatively, a separate thread can close() the ServerSocket. This is not documented but appears to be widely used. Of course the socket is unusable after close()ing it.
Finally, if you can afford to be bleeding edge look at JDK 1.4 java.nio.*. The new I/O packages feature non-blocking I/O.
- Peter
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Thanks Peter
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to interrupt a thread waiting for client connection? (URGENT)
 
Similar Threads
How to interrupt a thread waiting for client connection? (URGENT)
Thread confusion
NX: URLy Bird 1.3.1 Explicit Fatal Exception Handling
ServerSocket crash with Thread
Non-blocking sockets