微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java SocketChannel.read引发主机中的软件中止了已建立的连接

如何解决Java SocketChannel.read引发主机中的软件中止了已建立的连接

如果选择器认为它是可读的,我试图读取该通道。尽管如此,我还是得到了IOException:主机中的软件终止了已建立的连接。我面对它的原因可能是什么?

注意:我正在尝试制作一个VPN服务器。打开SocketChannel并使用WebSocket将数据传递到客户端。它可以在小型HTML页面中工作,但不能在大多数HTTP2页面中工作。那就是我得到这个“ IOException”的地方

注意:仅提供我面临错误的部分。我没有删除一些不相关的行。它是用科特林编写的。

while (true){
    var readyChannels = selector.selectNow()
    if(readyChannels == 0) continue

    val selectedKeys = selector.selectedKeys()
    val keyIterator = selectedKeys.iterator()
    while(keyIterator.hasNext()){
        val key = keyIterator.next()
        if(key.attachment() == null){
            keyIterator.remove()
            continue
        }
        val attachment: ChannelAttachment = key.attachment() as ChannelAttachment
        val channel = key.channel() as SocketChannel
        if(key.isValid){
            if(key.isReadable){
                val bb = ByteBuffer.allocate(8192)
                try{
                    while(channel.read(bb) != -1){
                        bb.flip()
                        var sendable = getSendable(Action.DATA,attachment.seq)
                        if(bb.remaining() <= 0) {
                            attachment.endThreshold--
                            if(attachment.endThreshold <= 0)
                                break
                            else
                                continue
                        }
                        attachment.endThreshold = DEFAULT_END_THRESHOLD
                        var dataString = bb.toBinaryByteString()
                        // Concatenate sendable and dataString
                        sendable += dataString
                        println("DATA: " + dataString.utf8())
                        println("data size: ${sendable.size} ${attachment.seq} ${bb.position()}")
                        Relay.ws?.send(sendable)
                        bb.clear()
                    }
                    if(!attachment.close){
                        attachment.close = true
                    }
                } catch (e: IOException){
                    // An established connection aborted
                    println(e.localizedMessage + " " + attachment.seq)
                    channel.close()
                    keyIterator.remove()
                    continue
                }
            }
            if(key.isValid and key.isWritable){
                if(TcpConnection.writePool.isNotEmpty()) println("writable ${attachment.seq} ${TcpConnection.writePool.size}")
                if(!attachment.written && TcpConnection.writePool.containsKey(attachment.seq)){
                    println("Writting ${attachment.seq}")
                    attachment.endThreshold = DEFAULT_END_THRESHOLD
                    try{
                        channel.write(ByteBuffer.wrap(TcpConnection.writePool[attachment.seq]))
                    } catch (e: IOException){
                        println("IOException occured during writting.")
                        println(e.message)
                    }
                    TcpConnection.writePool.remove(attachment.seq)
                    attachment.close = false
                }
            }
        }

        keyIterator.remove()
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。