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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...