How do you handle Socket Disconnecting in Java?

后端 未结 3 2026
暖寄归人
暖寄归人 2020-12-06 16:53

Hey all. I have a server written in java using the ServerSocket and Socket classes.

I want to be able to detect and handle disconnects, and then reconnect a new cl

3条回答
  •  我在风中等你
    2020-12-06 17:32

    Presumably, you're reading from the socket, perhaps using a wrapper over the input stream, such as a BufferedReader. In this case, you can detect the end-of-stream when the corresponding read operation returns -1 (for raw read() calls), or null (for readLine() calls).

    Certain operations will cause a SocketException when performed on a closed socket, which you will also need to deal with appropriately.

提交回复
热议问题