aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Socket event when client disconnects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket event when client disconnects?" Watch "Socket event when client disconnects?" New topic
Author

Socket event when client disconnects?

Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Hi

I am familiar with socket programming in C but not so much in Java. In the sockets C interface there is a socket event received by the socket server when a client disconnects (does a shutdown or close). But I can't find the equivalent for Java.net.

How would I code this in Java? Do I get an exception of a certain type? Something else?

Angus
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Your server will certainly get an exception if it tries to send data to a client which has already disconnected from the connection. I don't think the server gets notified until then, though. However it shouldn't be too hard to set up an experiment to find out for sure, should it?
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Paul Clapham wrote:Your server will certainly get an exception if it tries to send data to a client which has already disconnected from the connection. I don't think the server gets notified until then, though. However it shouldn't be too hard to set up an experiment to find out for sure, should it?


Looking on the web that was the impression I got, but wanted to check. Not getting such an event is a bit of a pain. I could set a timestamp for client activity and set a timer to periodically check if timestamp > a threshold and then send a short message to client to check - I guess that would work.
Java Wizard
Greenhorn

Joined: Apr 12, 2012
Posts: 5
java.io.EOFException will indicate the closing stream between sockets. This exception should help to identify the connection breakdown.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket event when client disconnects?