2017-05-29 94 views
0

我们有一个服务器与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但仍然没有运气 有人可以告诉我该怎么做吗?

+0

这没有任何意义。你已经拥有了一个'SSLSocket',你可以从中获得一个IP地址和端口来连接一个纯文本套接字通道,尽管实际上肯定对方会说SSL,而不是纯文本。究竟使用原始的SSLSocket是什么错误? – EJP

+0

@EJP:那么你有什么解决方案吗?我尝试了各种方法,但没有找到任何解决方案。如果你有任何解决方案将是非常有用的。 – Rohit

+0

目前尚不清楚你想要做什么。你为什么不使用SSLSocket来做你的阅读和写作? –

回答

1

我已按照this链接。并建议由EJP给出。

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

+1

感谢您的链接。非常有用:) –

+0

你'按照EJP给出的建议怎么样? – EJP