java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException
javax.net.ssl.SSLException
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
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.*;
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 ) {
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 ( "" ) ;
}
}