繁体   English   中英

无法从SocketChannel读取数据

[英]Not able to read data from SocketChannel

我们有一台带有https的服务器,该服务器运行在端口443上。我想从服务器读取字节。 我正在使用以下代码。

private void openAndConfigureChannel(Selector sel) throws IOException {
        SSLSocketFactory factory =
                (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslSocket =
                (SSLSocket) factory.createSocket(address,port);

        sslSocket.startHandshake();
        SocketChannel channel = SocketChannel.open();
        channel.connect(sslSocket.getRemoteSocketAddress());
        channel.configureBlocking(false);

        TCLog.i(TAG,"channel:"+channel);

        //channel.configureBlocking(false);
        channel.register(sel, channel.validOps());
    }

并且在读取数据时

private byte[] readData(SelectionKey key) throws Exception {
        SocketChannel socketChannel = (SocketChannel) key.channel();
        buf.clear();
        if (socketChannel .read(buf) == -1) {
            socketChannel .close();
            Log.i(TAG, "Error during read operation");
        }
        buf.flip();
        byte[] array = buf.array();
        int toPosition = buf.limit();
        byte[] subarray = ArrayUtils.subarray(array, 0, toPosition);
        return subarray;
    }

它给我异常,因为Connection reset by peer 在线socketChannel .read()

我尝试使用InetSocketAddress事件,但仍然没有运气有人可以告诉我该怎么做吗?

我已点击链接。 以及EJP的建议。

现在,我可以连接并读取数据了。

暂无
暂无

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

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