Tag Archives: java.io.EOFException

The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown

Don’t panic when you encounter this problem, go check the opinions. Let me summarize here

There are several situations where this problem exists

1. The ws connection is unstable and often disconnected;

answer:

1) It may be a network problem between the client and the server

2) The thread may be cleaned up abnormally due to insufficient memory on the server side

2. After the ws connection is successful, it will automatically disconnect after a while

answer:

1) In general, nginx forwarding or tomcat connection timeout causes the container layer to actively close the connection

(1) Modify the container layer tomcat or nginx [proxy_read_timeout 5000s ; keepalive_timeout 5000s ; ] configuration is enough , but it is not recommended to treat the symptoms rather than the root cause

(2) The server actively sends a heartbeat message to the client. It is not recommended because it will cause the server to load

(3) The client takes the initiative to send a heartbeat message to the server. I personally recommend this solution

3. After the ws connection, the server actively closes the connection due to various reasons such as authentication failure, which causes the exception to be thrown

Answer: The solution code is below [The key point is the CloseReason.CloseCodes.TLS_HANDSHAKE_FAILURE error code. When you use this error code, it will cause an io error, which means handshake failure]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
     * Operation for downline
     * @param session
     */
    public void optClose(Session session){
        // Determine if the current connection is still online
        if (session.isOpen()){
            try {
                // Closed
                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE,"Error!");
                session.close(closeReason);
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Note: When an abnormal error occurs, the connection should be closed in time and the related users’ online and offline operations should be handled!