2

I have written java client server socket connection code. In that the server accepts the client connection, then the server sends the data using PrintStream , and client reads the data in readline(). After this, the client will wait for the server in readline. If there is a network disconnection, the client keeps waiting in the readline and never exits from readline. Is there is a way to detect the disconnection from client side itself? Or whether I have to write new heartbeat thread to check network disconnection?

user3607195
  • 41
  • 1
  • 7

2 Answers2

1

Turn on socket option SO_KEEPALIVE on the socket. This will enable a heartbeat feature built into the TCP protocol.

Here's a simple way to turn this on.

Socket socket = ...; // Your socket on the client side
socket.setKeepAlive(true);

If the other side of the connection stops responding to the heartbeats, you will get an IOException on your call to read from the socket.

However, the timeout for this is 2 hours. This may be too long for your needs (you didn't specify)

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
-1

Disconnection from the client side:

  1. clientSocket.isConnected(): this is wrong, this will not return true and will keep returning false until the server has also disconnected
  2. You can try to send data through an output stream and you will get an exception, specifically IOException (Broken Pipe)
  3. You can also use socket.keepAlive(true) this will use the heartbeat feature built into the socket and you will get an exception because of it.

Cheers.

Community
  • 1
  • 1
nom
  • 256
  • 3
  • 16
  • (1) is not correct. This API does not magically detect disconnects and return false. (2) is correct. – user207421 Apr 03 '15 at 05:45
  • You will have to use another thread and keep checking for a `false` return value. – nom Apr 03 '15 at 05:47
  • No. `isConnected()` will *never* return false until *you* close the socket. Using another thread cannot change that. – user207421 Apr 03 '15 at 05:49
  • You're still wrong. The result of calling `isConnected()` at the client has nothing do with the server. And it returns immediately. You're just making this up. – user207421 Apr 03 '15 at 06:02
  • Please read this: http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected() – nom Apr 03 '15 at 06:05
  • It **Obviously** returns immediately. And if you read the javadoc you will see that I am not making this up. – nom Apr 03 '15 at 06:07
  • I've been reading it since 1997. There is nothing there about returning false when the peer disconnects. And obviously you have never tried it either. Your wording also states that it blocks, but possibly you have left a word out. Your answer is now so confused it is impossible to tell what you mean actually. All you need to do is *delete* (1) and your edit note. – user207421 Apr 03 '15 at 06:08
  • I see no improvement. Only more confusion. I suggest you start deleting things, not adding them. – user207421 Apr 03 '15 at 06:11
  • See, done. Now can we change our downvotes? – nom Apr 03 '15 at 06:16
  • Get it right first, and pay attention to what you're being told here. I repeat. The result of `isConnected()` has nothing to do with the peer, in this case the server. There is nothing in the Javadoc that says otherwise. You've never tried it. I have. You're making it up. It's wrong. – user207421 Apr 03 '15 at 06:18
  • `Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket` this I copied from the javadoc, http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected() care to explain what it means? – nom Apr 03 '15 at 06:20
  • It means what it says. I don't see the difficulty, or how it relates to your answer. If you have a question you should post it as a question, not in response to adverse comments about your wrong answer. This is not a forum. – user207421 Apr 03 '15 at 09:02