What to do when ServerSocket throws IOException and keeping server running

前端 未结 4 1972
一整个雨季
一整个雨季 2021-02-10 08:21

Basically I want to create a rock solid server.

while (keepRunning.get()) {
    try {
        Socket clientSocket = serverSocket.accept();

        ... spawn a n         


        
4条回答
  •  既然无缘
    2021-02-10 09:08

    Make sure you differentiate between different IOExceptions you might receive. Is it an exception on creating a connection? Is it an exception once a connection has already been established?

    The only code you gave is for accept()ing. Generally speaking, an IOException usually means an error on any layer on the physical network.

    Probably the best fallback behavior you can implement is to wait for a certain time quantum, and then try to reconnect. Assume you most possibly will not be able to reconnect, since you have lost network connection for more than a temporary period. Make sure you handle this gracefully. As @mdma mentioned, this must be supported by your clients as well.

提交回复
热议问题