无法从SocketChannel读取数据

时间:2017-05-29 11:40:14

标签: java android sockets sslsocketfactory

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

Gson gson = new GsonBuilder().setDateFormat("yyyy MM-dd'T'HH:mm:ss.SSS'Z'").create();

并在阅读数据时

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(),但仍然没有运气 有人能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:1)

我已关注this链接。以及EJP给出的建议。

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