Android - SSLSocket getInputStream很慢

时间:2014-01-30 18:45:07

标签: android ssl sslsocketfactory

编写我的第一个Android应用程序,它连接到我最近安装的家庭安全设备。我能够连接到设备并发送/接收数据,但是当我最初连接时,当我调用套接字getInputStream方法时,应用程序大约需要3-4秒。我认为这部分是因为它正在进行SSL握手。我想知道我能做些什么来加快这个速度吗?以下是我的代码:

KeyStore trustStore = KeyStore.getInstance("BKS");
InputStream trustStoreStream = _service.getResources().openRawResource(R.raw.mykeystore);
trustStore.load(trustStoreStream, "keystorepass".toCharArray());

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory factory = sslContext.getSocketFactory();
_socket = (SSLSocket) factory.createSocket(SERVER_IP, SERVER_PORT);
_socket.setUseClientMode(true);

if (_socket.isConnected()) {
    _nis = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
    _nos = new OutputStreamWriter(_socket.getOutputStream());

    String text;
    while((text = _nis.readLine()) != null){ //This is blocking
        messageReceived(text);
    }
}

0 个答案:

没有答案