Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket event when client disconnects?

 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.EOFException will indicate the closing stream between sockets. This exception should help to identify the connection breakdown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic