6

My project is using spring-boot web socket and embedded tomcat to implement chat server. Everything is ok but sometimes I got EOFException, and then the client cannot send a message to chat server until I restart tomcat then everything worked ok. I don't know when EOFException will happen. Pls help me

[TRACE] 2017-10-23 06:17:10.707 [http-nio-7755-exec-4] NativeWebSocketSession - Sending TextMessage payload=[{"result":..], byteCount=164, last=true], StandardWebSocketSession[id=42b, uri=/chat] [DEBUG] 2017-10-23 06:17:29.670 [http-nio-7755-exec-8] LoggingWebSocketHandlerDecorator - Transport error in StandardWebSocketSession[id=42b, uri=/chat] java.io.EOFException: null at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1242) ~[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1182) ~[tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:72) ~[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171) ~[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151) ~[tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148) [tomcat-embed-websocket-8.5.16.jar!/:8.5.16] at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_131] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar!/:8.5.16] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131] [DEBUG] 2017-10-23 06:17:29.671 [http-nio-7755-exec-8] LoggingWebSocketHandlerDecorator - StandardWebSocketSession[id=42b, uri=/chat] closed with CloseStatus[code=1006, reason=null]

Abdul Rasheed
  • 6,486
  • 4
  • 32
  • 48
Andy
  • 131
  • 1
  • 1
  • 4

2 Answers2

7

Yup, i solved it. This exception will happen after client or server was interrupted or stopped without calling close socket method(maybe lost internet, or shutdown laptop or mobile when using socket). So if we would like to solve this problem, we have to implement ping/pong mechanism, after interval if we cannot get pong response from client, we will close this socket. Another way, we can catch this exception, then we will close old socket.

Thanks, Andy

Andy
  • 131
  • 1
  • 1
  • 4
0

Well, I couldn't follow that. So here's my solution.

I was getting mysterious EOFExceptions when user sessions ended, and my solution was to beef up error handling (use a more detailed onError function) in the websocket onError method, like this.

I credit my rooting and rutting around javacodegeeks for eventually pointed me in the right direction.

@OnError
public void onError(Session sess, Throwable e) {
    Throwable cause = e.getCause();
    /* normal handling... */
    if (cause != null)
        System.out.println("Error-info: cause->" + cause);
    try {
        // Likely EOF (i.e. user killed session) 
        // so just Close the input stream as instructed
        sess.close();
    } catch (IOException ex) {
        System.out.println("Handling eof, A cascading IOException was caught: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        System.out.println("Session error handled. (likely unexpected EOF) resulting in closing User Session.");

    }
}
Marklan
  • 11
  • 3