devxlogo

How Do I Know When the “Handshake” Is Over?

How Do I Know When the “Handshake” Is Over?

The following code is a JSSE client application that uses the HandshakeCompletedListener interface to find out when a handshake is over.

import java.net.*;import java.io.*;import javax.net.*;import javax.net.ssl.*;class contact_server implements HandshakeCompletedListener{Socket SSLsoclu=null;          //client socketPrintWriter out=null;          //out streamBufferedReader in=null;        //in streamInputStreamReader ISR=null;String intrare=null;String iesire=null;BufferedReader message=null;  public contact_server(){}void send_message(){try{   SocketFactory SSLfabrica=SSLSocketFactory.getDefault();    SSLsoclu=SSLfabrica.createSocket("127.0.0.1",443);         ((SSLSocket)SSLsoclu).addHandshakeCompletedListener(this);  ((SSLSocket)SSLsoclu).startHandshake();         }catch(IOException e)       {       System.out.println("Error 1:"+e.getMessage());       System.exit(1);       }        try{      out=new PrintWriter(SSLsoclu.getOutputStream(),true);      ISR=new InputStreamReader(SSLsoclu.getInputStream());         in=new BufferedReader(ISR);                      while((intrare=in.readLine())!=null)       {           System.out.println("Server "+intrare);       message=new BufferedReader(new InputStreamReader(System.in));       System.out.flush();       System.out.print("Send:");       iesire=message.readLine();       if (iesire!=null)out.println(iesire);                                else break;       }      System.out.println("Connection canceled!!!");      out.close();      in.close();      message.close();      SSLsoclu.close();            }catch(IOException e)           {           System.out.println("Error 2:"+e.getMessage());           System.exit(1);           }  }  public void handshakeCompleted(HandshakeCompletedEvent e)     {     System.out.println(""Handshake" over!");     System.out.println(e.getCipherSuite());     }  }public class SSLClientSide{public static void main(String[] args){//put here your one keystore - this is just an exampleSystem.setProperty("javax.net.ssl.trustStore","SSLcert");System.setProperty("javax.net.ssl.trustStorePassword","e1002qa2");    contact_server t=new contact_server();    t.send_message();    }}

devx-admin

Share the Post: