Disconnecting Java Socket Connection from C # Disconnecting

in C #, when the socket connection is completed, another node is informed about this before ending the connection, so restoring the node can update the connection status.

in Java, when I complete the communication line, another node reports that the connection is valid.

Do I need to implement a read loop (it makes sense) that reports the connection as lost when it gets -1 while reading (in C # it is 0, I think)?

Thank you for your understanding.

EDIT: thanks to you both. as I suspected and mentioned in my post, additional verification is required to confirm the status of the connection.

+4
source share
2 answers

In java, you will find out that the other end of the socket is closed only when you read / write to / from the socket or request the state of the input stream (for example, InputStream.available() ). I do not think there is asynchronous notification that the other end is closed.

How do you verify that the socket is still open?

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

See InputStream.available ()

0
source

If the remote side of the connection goes away, you will usually get an IOException from the InputStream / InputChannel if a disconnect detection can be detected. If it cannot detect the disconnect, an IOException will eventually be thrown when the socket timeout. The length of time the wait timeout can be adjusted using Socket.setSoTimeout ().

+2
source

Source: https://habr.com/ru/post/1309351/


All Articles