简体   繁体   English

无法从SocketChannel读取数据

[英]Not able to read data from SocketChannel

We have a server with https and which runs on port 443. I want to read bytes form server. 我们有一台带有https的服务器,该服务器运行在端口443上。我想从服务器读取字节。 I am using following code. 我正在使用以下代码。

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());
    }

and while reading data 并且在读取数据时

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;
    }

It gives me exception as Connection reset by peer . 它给我异常,因为Connection reset by peer On line socketChannel .read() . 在线socketChannel .read()

Event I have tried with InetSocketAddress but still no luck Can somebody tell me how to do this? 我尝试使用InetSocketAddress事件,但仍然没有运气有人可以告诉我该怎么做吗?

I have Followed this link. 我已点击链接。 And suggestion given by EJP . 以及EJP的建议。

Now I am able to connect and read data. 现在,我可以连接并读取数据了。

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

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