title: SSLSocket hangs at getInputStream when android device is in wifi
body: <p>I want to have a SSL encrypted TCP server on the android device and a client on the computer which will connect to the device.</p>
  
  <p>I create a SSLServerSocket on the Android device with an own keystore. </p>
  
  <pre><code>final KeyStore localTrustStore = KeyStore.getInstance("BKS"); //NON-NLS
  final InputStream in = context.getResources().openRawResource(R.raw.syncapp);
  localTrustStore.load(in, "secret".toCharArray()); //Keystore pw
  in.close();
  
  final SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); //NON-NLS
  
  final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(localTrustStore);
  
  final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(localTrustStore, "secret".toCharArray()); //privat key pw
  
  sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
  
  serverSocket = sslContext.getServerSocketFactory().createServerSocket(SERVER_PORT);
  ((SSLServerSocket) serverSocket).setNeedClientAuth(true);
  </code></pre>
  
  <p>Then I wait for a client to connect. When a client wants to connect a new thread is started and the streams get demanded:</p>
  
  <pre><code>final DataInputStream input = new DataInputStream(this.clientSocket.getInputStream());
  final DataOutputStream output = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
  </code></pre>
  
  <p>First I used this code with USB-Tethering to gain a connection between the computer and the android device. So no Wifi/Network was enabled. Everything worked perfectly.</p>
  
  <p>Then I activated the wifi on the android device and connect to a wlan without internet.
  But now the call to getInputStream() seems to take 5 to 10 seconds.
  If I deactivate SSL it works perfectly.
  If the wlan does connect to the internet there is no delay as well.
  I tested this with Android 4.2 and 5.1.
  Update: Now I could test this issue with Android 6. And the issue seems to be fixed there...</p>
  
  <p>The Handshake is finished correctly but after that there seems to be some sort of delay on the android device. (The call to getInputStream consumes that time)
  Some devs are saying that it will do a DNS reverse lookup which will run into a timeout. </p>
  
  <p><a href="https://i.stack.imgur.com/BImIj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BImIj.png" alt="enter image description here"></a></p>
  
  <p>Take a look at the capture, the first connection was made while wifi was disabled. It took 0.3 sec to make the data transfer. Then I just activated the wifi, I didn't connect over the wifi, it still communicates over usb. And it took over 5 sec.</p>
  
  <p>I found the issue here as well, but they are using a client socket. I need a server socket. Does anyone have any idea how to fix this issue?</p>
  
  <p><a href="https://stackoverflow.com/questions/4737019/tls-connection-using-sslsocket-is-slow-in-android-os">TLS connection using SSLSocket is slow in Android OS</a></p>