我们有一个带有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,但仍然没有运气,谁能告诉我如何做到这一点?
发布于 2017-05-30 06:15:04
我跟踪了这链接。以及EJP提出的建议。
现在我能够连接和读取数据。
https://stackoverflow.com/questions/44241628
复制相似问题