views:

36

answers:

2

in C# when a sockets connection is terminated the other node is informed of this before terminating the link thus the remaning node can update the connection status.

in Java when i terminate a communication link the other node keeps reporting the connection as valid.

do i need to implement a read cycle (makes sense) that reports the connection as lost when it recieves a -1 during read (in C# this is 0 i think)?

thank you for your insight.

EDIT: thanks to you both. as i suspected and mentioned in my post that an additional check is required to confirm the connected state of a connection.

A: 

In java, you find out about the other end of the socket being closed only when you read/write to/from the socket, or query the input stream state (e.g. InputStream.available()). I don't think there is any asynchronous notification that the other end is closed.

How are you testing that the socket is still open?

You can poll the InputStream.available() method and if that returns -1, you know the socket is closed. Of course, you can also read data, if that fits with your usage.

See InputStream.available()

mdma
no, at the moment im playing around with ideas. this issue stood out as it functions differently than C#.
iEisenhower
+2  A: 

If the remote side of the connection goes away, normally you'll get an IOException from the InputStream/InputChannel if the disconnection can be detected. If it can't detect the disconnect an IOException will eventually be thrown when the socket read times out. The length of time it waits for a timeout can be adjusted using Socket.setSoTimeout().

Michael Barker