How to deal with open too many files error Socket is not properly closed under linux

1. First, use lsof -p PID to check the list of open files. If the following situation occurs, there are basically two possibilities. The stream is not closed or the socket is not closed, and the words "can't identify protocol" appear.

2. Use netstat -anp | grep PID to check the port occupancy status. If the following picture appears, it proves that the Socket is not closed.

reason:

  It is because of the Socket protocol itself. If a Socket is closed correctly, it requires 4 round-trip messages. If there are unreceived messages in the middle, it will stay in a certain state, such as the current CLOSE_WAIT.

Solution:

  Client: Actively close any socket, try to add socket.close() statement in finally.

  Server: Set the read timeout time such as: socket.setSoTimeout(3000); secondly, close it automatically after it is used up, the method is the same as that of the client. Also add socket.shutdownInput();socket.shutdownOutput(); to any exception

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326726606&siteId=291194637