Problem :exception occurs but program run
3 class SERVER CLIENT and MESSAGE BEING PASSED
///////////////////////message////////////1st make packge chat and import u can use message class in cLIENT AND server as welll
package chat;
import java.util.*;
import java.net.*;
import java.io.*;
public class Message implements Serializable
{
public String uname="name0";
public String data="data";
public String destination="destinatiom";
// public String sp[]={"ram"};
public String sp[];
public Hashtable<String,Socket> tl=new Hashtable<String,Socket>();
public Message()
{}
public Message(String dat)
{
data=dat;
}
public Message(String dest,String sms)
{
destination=dest;
data=sms;
}
}
/////////////////////////////////////////////////////server//////////////////
import chat.Message;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
// Java extension packages
import javax.swing.*;
public class Server extends JFrame implements Serializable
{
private JTextField enterField;
public JTextArea displayArea;
private ObjectOutputStream output;
ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int count = 1;
static Hashtable<String,Socket> h1=new Hashtable<String,Socket>();
// set up GUI///////////////////////////////////////////////////////////////////////////////////
public Server()
{
super( "Server" );
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( true );
////////////////////////////////////////////////
////////////////////////////////////////////////////
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
public void runServer()
{
try {
server = new ServerSocket( 5000, 100 );
while ( true )
{
displayArea.setText( "Waiting for connection\n" );
connection = server.accept();
try{
ObjectInputStream in=new ObjectInputStream(connection.getInputStream());
Message m =(Message)in.readObject();
String s= m.uname;
h1.put(s,connection);
sendList(h1);
Thread t = new Thread(new ClientHandler(connection) );
t.start();
}catch(Exception e)
{}
}
}
catch ( EOFException eofException )
{
System.out.println( "Client terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
/////////////////////////////////////////////////////////////////////
synchronized void sendData(Message m)throws IOException
{displayArea.append( "in send data");
String s=m.destination;
Set<String> set =h1.keySet();
Iterator<String> i= set.iterator();
while(i.hasNext())
{System.out.println(i.next());
String s1=i.next();
if(s1.equals(s))
{Socket ss=(Socket)h1.get(s1);
output=new ObjectOutputStream(ss.getOutputStream());
output.writeObject(m);
}
}
}
///////////////////////////////////////////////////
void sendList( Hashtable h1)
{
String so[]=new String[100];
int ii=0;
displayArea.append( "in list");
Message ms=new Message();
/////////////////
Set<String> set =h1.keySet();
Iterator<String> i= set.iterator();
while(i.hasNext())
{System.out.println(i.next());
String s1=i.next();
so[ii++]=s1;
}
ms.sp=so;
////////////////;
try{
while(i.hasNext())
{System.out.println(i.next());
String s1=i.next();
Socket ss=(Socket)h1.get(s1);
output=new ObjectOutputStream(ss.getOutputStream());
output.writeObject(ms);
}
output.flush();
}catch(IOException e)
{
System.out.println(e);
}
}
//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// execute application
public static void main( String args[] )
{
Server application = new Server();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
while(true)
{application.runServer();}
}
}
///////////////////////////////////
/////////////////////thread ////////////////////////
class ClientHandler implements Runnable
{
ObjectOutputStream out;
ObjectInputStream input;
Socket sock;
Server sr=new Server();
public ClientHandler(Socket clientSocket)
{
sock=clientSocket;
try
{out = new ObjectOutputStream( clientSocket.getOutputStream() );
Message m=new Message();
m.data="yes sir";
out.writeObject(m);
sr.displayArea.append( "Connection " + " received from: "+sock.getInetAddress() );
}catch(Exception e)
{}
} // end of constructor of ClientHandler class...
/////////////////////thread workiing/////////////////////////////////
public void run()
{
sr.displayArea.append( "in run");
try
{ Message n= new Message();
Message m1=(Message)input.readObject();
sr.h1.put(m1.uname,sock) ;
//boolean t=!(m1.destination.equals("destination"));
//boolean t1=!(m1.destination.equals("jatin"));
//f(t)
sr.sendData(m1);
}catch(Exception ex)
{
System.out.println("run : "+ex.toString() );
ex.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////client
import chat.Message;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class Client extends JFrame implements Serializable {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String chatServer;
private Socket client;
String name = JOptionPane.showInputDialog(null, "Enter some text : ", "enter name");
// initialize chatServer and set up GUI
public Client( String host )
{
super( "Client" );
// set server to which this client connects
chatServer = host;
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( false );
enterField.addActionListener(
new ActionListener() {
// send message to server
public void actionPerformed( ActionEvent event )
{
sendData( enterField.getText() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
// connect to server and process messages from server
public void runClient()
{
// connect to server, get streams, process connection
try {
// Step 1: Create a Socket to make connection
connectToServer();
// Step 3: Process connection
processConnection();
// Step 4: Close connection
closeConnection();
}
// server closed connection
catch ( EOFException eofException ) {
System.out.println( "Server terminated connection" );
}
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
// get streams to send and receive data
// connect to server
private void connectToServer() throws IOException
{
try { displayArea.setText( "Attempting connection\n" );
// create Socket to make connection to server
client = new Socket(
InetAddress.getByName( chatServer ), 5000 );
// display connection information
displayArea.append( "Connected to: " +
client.getInetAddress().getHostName() );
ObjectOutputStream os=new ObjectOutputStream(client.getOutputStream());
Message m=new Message();
m.uname=name;
m.destination="jatin";
m.data="ho gayay connect";
os.writeObject(m);
}catch(Exception e)
{System.out.println("hiiiiii");
}
}
// process connection with server
private void processConnection() throws IOException
{
// enable enterField so client user can send messages
enterField.setEnabled( true );
// process messages sent from server
Message message=new Message();
// read message and display it
try {
displayArea.append( "in process"+message.data );
String n[]=new String[100];
Message mn= ( Message ) input.readObject();
System.out.println(mn.uname+mn.data);
if (mn!=null)displayArea.append( "\n" + mn.uname+mn.data );
else System.out.println("aur khal ha");
if(mn.sp!=null)
{
n=mn.sp;
}
for(String z:n)
System.out.println(z);
displayArea.setCaretPosition(
displayArea.getText().length() );
}
// catch problems reading from server
catch ( Exception e )
{ System.out.println(e);
e.printStackTrace();
displayArea.append( "problem in server" );
}
} // end method process connection
// close streams and socket
private void closeConnection() throws IOException
{
displayArea.append( "\nClosing connection" );
output.close();
input.close();
client.close();
}
// send message to server
private void sendData( String me )
{
// send object to server do
while(!me.equals("\n"))
{
Message message=new Message();
message.data=me;
message.uname=name;
message.destination="jatin";
System.out.println(me+name);
try {
output.writeObject( message );
output.flush();
displayArea.append( "me" + message.data );
}catch ( IOException ioException ) {
displayArea.append( "\nError writing object" );
}
}
}
// execute application
public static void main( String args[] )
{
;
Client application;
if ( args.length == 0 )
application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
application.runClient();
}
}
[/B][/B][/B][/B][/B]