title: Android client and Java server communicating on SSL Socket
body: <p><strong>SERVER IMPLEMENTATION</strong></p>
  
  <pre><code>    SSLContext context = SSLContext.getInstance("SSL");
      KeyManagerFactory kmf =  
      KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      KeyStore ks = KeyStore.getInstance("JKS");
      char[] password = "daniere".toCharArray();
      ks.load(new FileInputStream("C:/Program Files/Java/jre7/bin/keystore.jks"),password);
      kmf.init(ks,password);
      context.init(kmf.getKeyManagers(), null, null);
      SSLServerSocketFactory factory = context.getServerSocketFactory();
      SSLServerSocket server = (SSLServerSocket)factory.createServerSocket(1000);
      System.out.println("Server ready for conncetions...");
      while (true)
      {
       SSLSocket soc = (SSLSocket) server.accept();
      BufferedReader key = new BufferedReader(new InputStreamReader(soc.getInputStream()));
  
      String c = null;
          while((c=key.readLine())!=null)
          {
              System.out.println(c);
              break;
          }
  
         }
  </code></pre>
  
  <p><strong>CLIENT IMPLEMENTATION</strong> </p>
  
  <pre><code>    SSLContext sslcontext = SSLContext.getInstance("SSL");
      KeyStore ks = KeyStore.getInstance("BKS");
      TrustManagerFactory trustManagerFactory =  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      InputStream in = getClass().getResourceAsStream("/assets/trust.bks");
      ks.load(in,"daniere".toCharArray());
      trustManagerFactory.init(ks);
      sslcontext.init(null, trustManagerFactory.getTrustManagers(), null);
      SSLSocketFactory sf = sslcontext.getSocketFactory();
      SSLSocket s1 = (SSLSocket) sf.createSocket("10.0.0.1", 1000);
      String key = "sdsdsd";
      PrintWriter sockey = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())), true);
      sockey.println(key);
      s1.close;
  </code></pre>
  
  <p>i have self created certificate and keystore an truststore. The Android client just has to authenticate if is speaking to the proper server.Client need not authenticate to the server.</p>
  
  <p><strong>ERRORS</strong></p>
  
  <p>Server ready for conncetions...</p>
  
  <p>Server connected to :/10.0.0.2:45120  /10.0.0.1:1000</p>
  
  <p>javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
      at sun.security.ssl.AppInputStream.read(Unknown Source)
      at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
      at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
      at sun.nio.cs.StreamDecoder.read(Unknown Source)
      at java.io.InputStreamReader.read(Unknown Source)
      at java.io.BufferedReader.fill(Unknown Source)
      at java.io.BufferedReader.readLine(Unknown Source)
      at java.io.BufferedReader.readLine(Unknown Source)
      at SSL.SSLserverThread.run(SSLserverThread.java:75)
  Caused by: java.io.EOFException: SSL peer shut down incorrectly
      at sun.security.ssl.InputRecord.read(Unknown Source)
      ... 12 more</p>
  
  <p>I am not able to figure out why the client closes the socket at handshake?????
  I have spent a lot of time  searching about this problem with no luck. I am new in this so </p>
  
  <p>any help appreciated .</p>
  
  <p>I tried this server/client ssl socket on pure java and it worker perfectly..
  When try to implement on Android as a client no success.
  Thanks alot.</p>