简体   繁体   中英

Java SocketChannel didnt throw IOException on linux environment

I try to implement server-client application and I connect to server succesfully but when the server is closed, client doesnt throw IOException (java.io.IOException: An existing connection was forcibly closed by the remote host) from channel.finishConnect() or channel.read() methods on linux environment which I expected.

I test the code on windows environment and it works well.

 protected void mainLoop() {
    int count;
    String message = "";
    long connStartTs = 0, connEndTs = 0;
    while (true) {
        try {

            if (activeCommand == ConnectionCommand.DISCONNECT_AND_LOGOUT) {

                disconnect();
            } else if (activeCommand == ConnectionCommand.CONNECT_AND_LOGIN) {
                openChannel();
                if (connecting && (connEndTs - connStartTs) > cGlobals.CLIENT_SOCKET_TIMEOUT) {
                    throw new Exception("Connection timed out");
                }
                if (channel.isConnectionPending() && !channel.finishConnect()) {
                    continue;
                }
                if (!channel.isConnected()) {
                    ..
                } else if (!logonSent && channel.isConnected()) {
                    ..
                } else if (logonSent && channel.isConnected()) {
                    ByteBuffer buffer = ByteBuffer.allocate(20000);
                    count = 0;
                    while ((count = channel.read(buffer)) > 0) {
                        buffer.flip();
                        processSocketData(Charset.defaultCharset().decode(buffer));
                    }
                   ..
                }
            }
            Thread.sleep(10);
        } catch (Exception exc) {
            Log4Me.getLogger().info(Log4Me.exceptionMessage(exc));
            try {
                channel.close();
            } catch (IOException ex) {
                Log4Me.getLogger().error(Log4Me.exceptionMessage(exc));
            }
        }
    }
}

Do you have any idea for this?

The text of IOExceptions comes from the operating system in most cases. You shouldn't rely on it as a cross-platform feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM