简体   繁体   English

java.nio.channels.ClosedChannelException

[英]java.nio.channels.ClosedChannelException

How can i solve this problem. 我怎么解决这个问题。 I got following error: 我收到以下错误:

java.nio.channels.ClosedChannelException

This is coding: 这是编码:

 public void run() {

    try {
        SocketChannel socketChannel = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.allocate(512);
        int i1 = socketChannel.read(buffer);

        if (buffer.limit() == 0 || i1 == -1) {

            Socket s = null;
            try {
                s = socketChannel.socket();
                s.close();
                key.cancel();
            } catch (IOException ie) {
                if (UnitDataServer.isLog) {
                    log.error("Error closing socket " + s + ": " + ie);
                }
            }
        } else {
            buffer.flip();
            if (UnitDataServer.isLog) {
                log.info(" Recvd Message from Unit : " + buffer.array());
            }
            byte byteArray[] = buffer.array();
            log.info("Byte Array length :" + byteArray.length);
            hexString = new StringBuffer();

            for (int i = 0; i < i1 /* byteArray.length */; i++) {
                String hex = Integer.toHexString(0xFF & byteArray[i]);
                if (hex.length() == 1) {
                    // could use a for loop, but we're only dealing with a
                    // single byte
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            hexString.trimToSize();
            log.info("Hex String :" + hexString);

             Communicator.dataReceive(new  DataReceive(
                    socketChannel, hexString.toString(), dst));

        }
    } catch (Exception e) {
        if (UnitDataServer.isLog) {
            // log.error(e);
        }
        try {
            socketChannel.socket().close();
            key.cancel();
        } catch (IOException ex) {
            if (UnitDataServer.isLog) {
                log.error(ex);
            }
        }
    }
}

You have closed the channel and are still trying to use it. 您已关闭频道并仍在尝试使用它。

There are several issues with your code. 您的代码有几个问题。

First, your test for EOS is faulty. 首先,您对EOS的测试是错误的。 Remove the limit() == 0 test. 删除limit() == 0测试。 That doesn't indicate EOS, it just indicates a zero length read, which can happen in non-blocking mode at any time. 这并不表示EOS,它只是指示零长度读取,这可以在任何时间以非阻塞模式发生。 It doesn't mean the peer has closed his end of the connection, and it doesn't mean you should close your end. 这并不意味着同伴关闭了他的连接结束,并不意味着你应该关闭你的结束。

Second, closing a channel closes the socket as well. 其次,关闭通道也会关闭插座。 You should close the channel only, not the socket. 您应该只关闭通道,而不是套接字。

Third, closing a channel cancels the key. 第三,关闭频道会取消密钥。 You don't need to follow every close with a cancel. 您不需要通过取消来关注每一个关闭。

You may also have failed to check whether a ready key is valid in the select loop before using it, eg for reading. 您可能还没有在使用之前检查就绪键是否在选择循环中有效,例如用于读取。

I continue to be amazed, and amused, and bemused, by the claim elsewhere in this thread that 'source code is untrue' under some circumstances. 我继续对这个帖子中其他地方的声明感到惊讶,感到好笑和困惑,在某些情况下“源代码是不真实的”。

You need to fix/secure code that is throwing this exception. 您需要修复/保护抛出此异常的代码。 ClosedChannelException is ... ClosedChannelException是......

... thrown when an attempt is made to invoke or complete an I/O operation upon channel that is closed, or at least closed to that operation. ...当尝试在关闭或至少关闭该操作的通道上调用或完成I / O操作时抛出... That this exception is thrown does not necessarily imply that the channel is completely closed. 抛出此异常并不一定意味着通道完全关闭。 A socket channel whose write half has been shut down, for example, may still be open for reading 例如,其写半部分已被关闭的套接字通道仍可以打开以供读取

(as described in Java 6 API ) (如Java 6 API中所述

But really, you would need to provide us code snipped and stack trace in order to get more detailed help. 但实际上,您需要为我们提供代码剪切和堆栈跟踪,以获得更详细的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SSL和Postman的java.nio.channels.ClosedChannelException - java.nio.channels.ClosedChannelException with SSL and Postman java.nio.channels.ClosedChannelException HTTP/2 与码头 - java.nio.channels.ClosedChannelException HTTP/2 with jetty 异常-java.nio.channels.ClosedChannelException - exception - java.nio.channels.ClosedChannelException jenkins主连接失败,出现java.nio.channels.ClosedChannelException - jenkins master connection fails with java.nio.channels.ClosedChannelException Stardog Connection.commit()引发java.nio.channels.ClosedChannelException - Stardog Connection.commit() raising java.nio.channels.ClosedChannelException java.nio.channels.ClosedChannelException-客户端关闭SSL - java.nio.channels.ClosedChannelException -Client shuts down SSL GATLING Rest API 测试 - java.nio.channels.ClosedChannelException: Z3099A62586648C1 - GATLING Rest API testing - java.nio.channels.ClosedChannelException: null Apache Kafka:无法更新Metadata / java.nio.channels.ClosedChannelException - Apache Kafka: Failed to Update Metadata/java.nio.channels.ClosedChannelException 在Play应用程序中获取java.nio.channels.ClosedChannelException异常 - Getting java.nio.channels.ClosedChannelException excetion in Play Application 写入客户端通道时出现异常java.nio.channels.ClosedChannelException - Exception java.nio.channels.ClosedChannelException when write to client channel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM