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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
Thanks for your registration, follow us on our social networks to keep up-to-date