Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Apr 25, 2006

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 socket
PrintWriter out=null;          //out stream
BufferedReader in=null;        //in stream
InputStreamReader 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 example
System.setProperty("javax.net.ssl.trustStore","SSLcert");
System.setProperty("javax.net.ssl.trustStorePassword","e1002qa2");

    contact_server t=new contact_server();
    t.send_message();
    }
}
DevX Staff
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap