我們有一個服務器與https,並在端口443上運行。我想讀取字節表單服務器。 我正在使用以下代碼。無法從SocketChannel讀取數據
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
但仍然沒有運氣 有人可以告訴我該怎麼做嗎?
這沒有任何意義。你已經擁有了一個'SSLSocket',你可以從中獲得一個IP地址和端口來連接一個純文本套接字通道,儘管實際上肯定對方會說SSL,而不是純文本。究竟使用原始的SSLSocket是什麼錯誤? – EJP
@EJP:那麼你有什麼解決方案嗎?我嘗試了各種方法,但沒有找到任何解決方案。如果你有任何解決方案將是非常有用的。 – Rohit
目前尚不清楚你想要做什麼。你爲什麼不使用SSLSocket來做你的閱讀和寫作? –