2

Writing my first Android app, which is connecting to a home security device I recently installed. I'm able to connect to the device and send/receive data but when I initially connect, the app takes about 3-4 seconds when I call the socket getInputStream method. I assume this is partly because it's doing the SSL handshake. I'm wondering if there is something I can do to speed this up? Below is my code:

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);
    }
}
Gentenator
  • 207
  • 2
  • 11
  • It's the handshake. Not much you can do about it. You don't need to call setUseClientMode(true): it is the default. – user207421 Jan 30 '14 at 22:12
  • May be this is related to a DNS reverse lookup? I have similar issue: http://stackoverflow.com/questions/37717585/sslsocket-hangs-at-getinputstream-when-android-device-is-in-wifi http://stackoverflow.com/questions/4737019/tls-connection-using-sslsocket-is-slow-in-android-os?lq=1 – Rocket Jun 09 '16 at 05:35

0 Answers0