我们有一个带有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
但仍然没有运气的事件有人可以告诉我怎么做吗?
我已经点击了这个链接。以及EJP给出的建议。
现在我能够连接和读取数据。