java - 无法从 SocketChannel 读取数据

标签 java android sockets sslsocketfactory

我们有一个带有 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;
    }

它给了我一个异常,因为连接被同行重置。上线socketChannel.read()

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

最佳答案

我已关注this关联。以及EJP给出的建议.

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

关于java - 无法从 SocketChannel 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241628/

相关文章:

android - 从与 SimpleCursorAdapter 绑定(bind)的 ListView 中获取所选项目

c++ - 跟踪 Flash 应用程序数据包

jquery - io.socket.emit 返回未定义

无法计算 C 中插槽的回合数

java - 检查循环中的新文件 - java

java - 尽可能晚或在它们所属的最近的大括号中声明局部变量?

java - 类无法转换为 ParseObject

java - 使用 javax.lang.model 或 ANTLR JavaParser 获取 Java 源代码信息的示例/教程

java - HashMap containsKey 方法

Android ffmpeg 版本 :invalid address or address of corrupt block passed to dlfree