无法从 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 尝试过,但仍然没有成功 有人可以告诉我该怎么做吗?

我已关注 this link. And suggestion given by EJP

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