首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从SocketChannel读取数据

无法从SocketChannel读取数据
EN

Stack Overflow用户
提问于 2017-05-29 11:40:13
回答 1查看 830关注 0票数 0

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

代码语言:javascript
复制
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());
    }

在读取数据时

代码语言:javascript
复制
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,但仍然没有运气,谁能告诉我如何做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-30 06:15:04

我跟踪了链接。以及EJP提出的建议。

现在我能够连接和读取数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44241628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档