KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > net > ssl > SSLHandshakeException

javax.net.ssl
Class SSLHandshakeException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.io.IOException
              extended by javax.net.ssl.SSLException
                  extended by javax.net.ssl.SSLHandshakeException
All Implemented Interfaces:
Serializable
See Also:
Source Code

public SSLHandshakeException(String reason)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[422]Socket Chat Swing application
By amit on 2003/09/29 07:59:21  Rate
/* 
  * @ ( # ) SocketChatSwing.java 
  * 
  * Copyright  ( c )  2003-2004 Greet Incorporated 
  * 337, Raheja Arcade, Koramangala, Bangalore,Karnataka,India 
  * All rights reserved. 
  * 
  * This software is the confidential and proprietary information of Greet Incorporated 
    ( "Confidential Information" ) . You shall not 
  * disclose such Confidential Information and shall use it only in 
  * accordance with the terms of the license agreement you entered into 
  * with Greet. 
  */
 
  
  
  
  
  
 import java.awt.*; 
 import java.awt.event.*; 
 import javax.swing.*; 
 import java.io.*; 
 import java.net.*; 
 import java.util.*; 
 import javax.net.ssl.*; 
 import java.security.*; 
  
  
  /** 
  * Class description goes here. 
  * @version   1 
  * @author Akshatha.S.Shetty 
  * Class for the chat interface 
  */
 
  
  
 public class SocketChatSwing extends JFrame implements ActionListener 
  {  
 static final int HTTPS_PORT = 443; 
  
  
          DataInputStream serverStream; 
          Socket client; 
          JTextField textField, login; 
          JTextArea textArea; 
          String name, text; 
  
  
     public static void main ( String [  ]  args )  {  
       /*Constructor*/ 
          SocketChatSwing swing = new SocketChatSwing (  ) ; 
  
  
            }  
  
  
     public SocketChatSwing (  )  
       {  
  
  
          JLabel LoginName = new JLabel ( "Enter Name" ) ; 
          login = new JTextField ( 10 ) ; 
          LoginName.setLabelFor ( login ) ; 
          JLabel Chat = new JLabel ( "Start Chat" ) ; 
  
  
          textField = new JTextField ( 20 ) ; 
          Chat.setLabelFor ( textField ) ; 
          textField.addActionListener ( this ) ; 
  
  
          textArea = new JTextArea ( 5, 20 ) ; 
          textArea.setEditable ( false ) ; 
          JScrollPane scrollPane = new JScrollPane ( textArea, 
                                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ) ; 
  
  
  
          GridBagLayout gridBag = new GridBagLayout (  ) ; 
          Container contentPane = getContentPane (  ) ; 
          contentPane.setLayout ( gridBag ) ; 
          GridBagConstraints c = new GridBagConstraints (  ) ; 
          c.gridwidth = GridBagConstraints.REMAINDER; 
  
  
          c.fill = GridBagConstraints.HORIZONTAL; 
          gridBag.setConstraints ( login, c ) ; 
          contentPane.add ( LoginName ) ; 
          contentPane.add ( login ) ; 
  
  
  
          c.fill = GridBagConstraints.HORIZONTAL; 
          gridBag.setConstraints ( textField, c ) ; 
          contentPane.add ( Chat ) ; 
          contentPane.add ( textField ) ; 
  
  
          c.fill = GridBagConstraints.BOTH; 
          c.weightx = 1.0; 
          c.weighty = 1.0; 
          gridBag.setConstraints ( scrollPane, c ) ; 
          contentPane.add ( scrollPane ) ; 
  
  
          setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ; 
          setContentPane ( contentPane ) ; 
  
  
          pack (  ) ; 
          setVisible ( true ) ; 
  
  
       }  
  
  
  /*-------------------------------------------------------------------------------------------------*/ 
  
  
   void broadcastMessage ( String message )  
    {  
        name = login.getText (  ) ; 
        message = URLEncoder.encode ( name + ": " + message ) ; // Pre-pend the speaker's name 
        try  {  
              System.setProperty ( "javax.net.ssl.trustStore", "cacerts" ) ; 
              System.setProperty ( "javax.net.ssl.trustStorePassword","changeit" ) ; 
              URL url = new URL ( "https://Akshata:8443/demochat/servlet/ChatServlet?message="+message ) ; 
              HttpsURLConnection con =  ( HttpsURLConnection )  url.openConnection (  ) ; 
  
  
              //URLConnection con = url.openConnection (  ) ; 
              con.setDoInput ( true ) ; 
              con.setDoOutput ( true ) ; 
              con.setUseCaches ( false ) ; 
              con.connect (  ) ; 
              BufferedReader br = new BufferedReader 
                                  ( new InputStreamReader ( con.getInputStream (  )  )  ) ; 
  
  
  
             }  
        catch  ( Exception e )   {  
              // Can't connect to host, report it and abandon the broadcast 
              System.out.println ( "Can't connect to host: " + e.getMessage (  )  ) ; 
             }  
  
  
  
     }  
  
  
    /*-------------------------------------------------------------------------------------------*/ 
  
  
   String getmessage (  )  
      {  
        String ss = null; 
  
  
        while  ( ss == null )   {  
              try  {  
                   // Connect to the server if we haven't before 
                   if  ( serverStream == null )   {  
  
  
                   //System.setProperty ( "javax.net.ssl.trustStore", "cacerts" ) ; 
                   //System.setProperty ( "javax.net.ssl.trustStorePassword","changeit" ) ; 
                   SSLSocketFactory sslsocketfactory =  ( SSLSocketFactory ) SSLSocketFactory.getDefault (  ) ; 
                   SSLSocket s =  ( SSLSocket ) sslsocketfactory.createSocket ( "Akshata", HTTPS_PORT ) ; 
                   s.startHandshake (  ) ; 
                   s.setTcpNoDelay ( true ) ; 
                   //Socket s = new Socket ( "Akshata", PORT ) ; 
                   serverStream = new DataInputStream (  
                                  new BufferedInputStream (  
                                  s.getInputStream (  )  )  ) ; 
                                   }  
  
  
                   // Read a line 
                   ss = serverStream.readLine (  ) ; 
                   }  
  
  
              catch  ( SocketException e )   {  
                   // Can't connect to host, report it and wait before trying again 
                   System.out.println ( "Cannot connect to host: " + e.getMessage (  )  ) ; 
                   serverStream = null; 
                   try  {  
                       Thread.sleep ( 5000 ) ; 
                        }  
                   catch  ( InterruptedException ignored )   {   }  
                   }  
              catch  ( SSLHandshakeException reason )  {  
                   System.out.println ( "Handaashake Exception :" +reason.getCause (  ) + ":" + reason.getMessage (  )  ) ; 
                  }  
              catch  ( Exception e )   {  
                   // Some other problem, report it and wait before trying again 
                   System.out.println ( "General exception: " + 
                   e.getClass (  ) .getName (  )  + ": " + e.getMessage (  )  ) ; 
  
  
                         try  {  
                         Thread.sleep ( 1000 ) ; 
                          }  
  
  
                         catch  ( InterruptedException ignored )   {   }  
                }  
  
  
          }  
           return ss + "\n"; 
      }  
  
  
   /*----------------------------------------------------------------------------------------------*/ 
  
  
     public void actionPerformed ( ActionEvent evt )  
      {  
                    broadcastMessage ( textField.getText (  )  ) ; 
                    textArea.append ( getmessage (  )  ) ; 
                    textField.setText ( "" ) ; 
  
  
      }  
  }  
  
  
  
 /*-----------------------------------End of File--------------------------------------------------*/

Popular Tags