What to do when ServerSocket throws IOException and keeping server running

前端 未结 4 1974
一整个雨季
一整个雨季 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:16

    You can get an IOException on an accept() if the server socket is closed (by you) or you run out of resources, e.g. file handles. Either way, there is not much you can do about it. If the serverSocket is closed (you can test for this) you probably had a good reason to do this. If you run out of resources, you will either have to increase your resource limit, which requires a restart of your application, or you have a resource leak.

提交回复
热议问题