Thursday, January 12, 2017

Windows: java SSL socket connections sample code

1. The SSL server socket

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

public class SSLServer {

    public static void main(String args[]) {
        //set which keystore to use
        System.setProperty("javax.net.ssl.keyStore", "<path>/<keystore file name>");
        System.setProperty("javax.net.ssl.keyStorePassword", "<keystore password>");

        try {
            SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            //create a server socket listening on a specified port.
            ServerSocket ss = ssf.createServerSocket(<port>);
            while (true) {
                //accept connections from client
                Socket s = ss.accept();
                //SSLSession session = ((SSLSocket) s).getSession();
                //Certificate[] cchain2 = session.getLocalCertificates();
               // for (int i = 0; i < cchain2.length; i++) {
                 //   System.out.println(((X509Certificate) cchain2[i]).getSubjectDN());
              //  }
               // System.out.println("Peer host is " + session.getPeerHost());
              //  System.out.println("Cipher is " + session.getCipherSuite());
               // System.out.println("Protocol is " + session.getProtocol());
               // System.out.println("ID is " + new BigInteger(session.getId()));
              //  System.out.println("Session created in " + session.getCreationTime());
              //  System.out.println("Session accessed in " + session.getLastAccessedTime());

                //the reader
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                //the wirter
                PrintStream out = new PrintStream(s.getOutputStream());
                String message;
                while (!(message = in.readLine()).equals("end")) {
                      if (message.equals("Got it?")){
                            out.println("Yes, received.");
                      } else if (message.equals("Hello")) {
                            out.println("Hi");
                      } else if (message.equals("Thanks")){
                            out.println("end");
                      }
                }
                out.close();
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }
}

2. SSL client socket

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigInteger;
import java.net.Socket;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class SSLClient {
    public static void main(String args[]) throws Exception {
          System.setProperty("javax.net.ssl.trustStore", "<path>/<truststore file name>");
          System.setProperty("javax.net.ssl.keyStorePassword", "<truststore password>");

          SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
          Socket s = ssf.createSocket("<server IP address>", <port>);

         // SSLSession session = ((SSLSocket) s).getSession();
         // Certificate[] cchain = session.getPeerCertificates();
          //System.out.println("The Certificates used by peer");
          //for (int i = 0; i < cchain.length; i++) {
                //  System.out.println(((X509Certificate) cchain[i]).getSubjectDN());
         // }
         // System.out.println("Peer host is " + session.getPeerHost());
         // System.out.println("Cipher is " + session.getCipherSuite());
         // System.out.println("Protocol is " + session.getProtocol());
         // System.out.println("ID is " + new BigInteger(session.getId()));
          //System.out.println("Session created in " + session.getCreationTime());
        // System.out.println("Session accessed in " + session.getLastAccessedTime());

         PrintStream out = new PrintStream(s.getOutputStream());
         out.println("Hello");
         BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
         String x;
         while(!(x = in.readLine()).equals("end")){
               System.out.println(x);
               if (x.equals("Hi")){
                     out.println("Got it?");
               } else if (x.equals("Yes, received.")){
                     out.println("Thanks");
                }
          }
         out.println("end");
         in.close();
         out.close();
         s.close();
     }
}

---------------------------------------------------------------------------------------------------

                        


If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Reference:
1. Use SSLServerSocketFactory to create a SSL Server

No comments:

Post a Comment