title: Why does an SSL Server Socket connection block in Java whereas a non SSL Server Socket does not?
body: <p>Consider the following code for a non-SSL Socket server and client all on the one thread:</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class ServerClient {
      public static void main(String[] args) throws IOException {
          ServerSocket ss = new ServerSocket(0); // open a random free port.
  
          Socket c = new Socket(ss.getInetAddress(), ss.getLocalPort());
  
          Socket s = ss.accept();
  
          final byte[] bytes = "Hello World!".getBytes();
          final OutputStream out = c.getOutputStream();
          System.out.println("writing to stream");
          out.write(bytes.length);
          out.write(bytes);
  
          System.out.println("reading from stream");
  
          final DataInputStream in = new DataInputStream(s.getInputStream());
          int len = in.read();
          final byte[] b = new byte[len];
          in.readFully(b);
          System.out.println(new String(b));
  
          c.close();
          ss.close();
      }
  }
  </code></pre>
  
  <p>This produces the following output:</p>
  
  <pre><code>writing to stream
  reading from stream
  Hello World!
  </code></pre>
  
  <p>This process opened a server socket - connected with a client socket. Passed data down the socket and then closed down. There was no issue passing the data. </p>
  
  <p>Consider a version to a prove a point with SSL Sockets:</p>
  
  <pre><code>import javax.net.ssl.SSLServerSocket;
  import javax.net.ssl.SSLServerSocketFactory;
  import javax.net.ssl.SSLSocket;
  import javax.net.ssl.SSLSocketFactory;
  import java.io.DataInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.Socket;
  import java.util.logging.Logger;
  
  public class SSLServerClient {
  
      private static Logger log = Logger.getLogger("InfoLogging");
  
      public static void main(String[] args) throws IOException {
  
          System.setProperty("javax.net.ssl.keyStore", "/path/KeyStore.jks");
          System.setProperty("javax.net.ssl.keyStorePassword", "password");
  
          SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  
          SSLServerSocket serverListeningSSLSocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(4380);
          log.info("Server started");
  
          SSLSocketFactory sslSocketFactory=(SSLSocketFactory) SSLSocketFactory.getDefault();
          SSLSocket clientSocket =  (SSLSocket) sslSocketFactory.createSocket(serverListeningSSLSocket.getInetAddress(),
                  serverListeningSSLSocket.getLocalPort());
  
          SSLSocket serverCommsSSLSocket = (SSLSocket) serverListeningSSLSocket.accept();
          log.info("new client");
  
          final byte[] bytes = "Hello World!".getBytes();
          final OutputStream out = clientSocket.getOutputStream();
          System.out.println("writing to stream");
          out.write(bytes.length);
          out.write(bytes);
  
          System.out.println("reading from stream");
  
          final DataInputStream in = new DataInputStream(serverCommsSSLSocket.getInputStream());
          int len = in.read();
          final byte[] b = new byte[len];
          in.readFully(b);
          System.out.println(new String(b));
  
          clientSocket.close();
          serverCommsSSLSocket.close();
          serverListeningSSLSocket.close();
      }
  }
  </code></pre>
  
  <p>This gives the following output:</p>
  
  <pre><code>Nov 21, 2018 10:23:51 PM com.gamble.ssl.SSLServerClient main INFO: Server started
  Nov 21, 2018 10:23:52 PM com.gamble.ssl.SSLServerClient main INFO: new client
  writing to stream
  </code></pre>
  
  <p>ie it blocks on the server socket starting. </p>
  
  <p>My question is: <strong>Why does an SSL Server Socket connection block in Java whereas a non SSL Server Socket does not?</strong></p>