Communication between the clients from Server
//Chat Server:- import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class ChatServer extends Frame implements ActionListener{ Label listOfClients; List clientName; Button exit; Panel p1,p2; ServerSocket ss; Clients clients[]; int n; boolean sendOk=true; boolean update=false; ChatServer(){ super("ChatServer"); setSize(400,300); setVisible(true); clientName=new List(); listOfClients=new Label("List of Clients"); exit=new Button("Exit"); p1=new Panel(); p2=new Panel(); p1.setLayout(new FlowLayout()); p2.setLayout(new FlowLayout()); setLayout(new GridLayout(2,1)); add(p1); add(p2); p1.add(listOfClients); p1.add(clientName); p2.add(exit); exit.addActionListener(this); try{ ss=new ServerSocket(2500); }catch(Exception e){ } clients=new Clients[50]; n=0; new CheckNewClients().start(); new UpdateClients().start(); } public void actionPerformed(ActionEvent ae){ System.exit(0); } public static void main(String args[]){ new ChatServer(); } class Clients extends Thread{ Socket s; BufferedReader br; PrintWriter pw; String name="Client "; int i; boolean alive; public void run(){ try{ while(true){ String temp=br.readLine(); if(temp.equals("shutdown")){ for(int x=0;x<clientName.getItemCount();x++) if(clientName.getItem(x).equals(name)){ clientName.remove(x); break; } alive=false; update=true; break; } else if(temp.equals("msg")){ String client=br.readLine(); String msg=br.readLine(); int x; for(x=0;x<n;x++) if(clients[x].name.equals(client)) break; while(!sendOk); sendOk=false; clients[x].pw.println("msg"); clients[x].pw.println(name+" : "+msg); sendOk=true; } } }catch(Exception e){ for(int x=0;x<clientName.getItemCount();x++) if(clientName.getItem(x).equals(name)){ clientName.remove(x); break; } alive=false; update=true; } } } class CheckNewClients extends Thread{ public void run(){ while(true){ clients[n]=new Clients(); try{ clients[n].s=ss.accept(); clients[n].br=new BufferedReader(new InputStreamReader(clients[n].s.getInputStream())); clients[n].pw=new PrintWriter(clients[n].s.getOutputStream(),true); clients[n].i=n; clients[n].name+=n; clients[n].alive=true; clientName.add(clients[n].name); clients[n].pw.println(clients[n].name); clients[n].start(); update=true; }catch(Exception e){ System.out.println(e); } n++; } } } class UpdateClients extends Thread{ public void run(){ while(true){ while(!update); for(int i=0;i<n;i++){ int count=clientName.getItemCount(); if(!clients[i].alive) continue; try{ while(!sendOk); sendOk=false; clients[i].pw.println("update"); clients[i].pw.println(count-1); for(int x=0;x<count;x++) if(!clientName.getItem(x).equals(clients[i].name)) clients[i].pw.println(clientName.getItem(x)); sendOk=true; }catch(Exception e){ System.out.println(e); } } update=false; } } } } //Chat Client:- import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class ChatClient extends Frame implements ActionListener{ Label enterMessage,to,receivedMessages,sentMessages; TextField enterMessaget; TextArea receivedMessagest,sentMessagest; Button send,exit; Choice clientName; Panel p1,p2,p3,p4; Socket s; PrintWriter pw; BufferedReader br; ChatClient(){ super("ChatClient"); setVisible(true); setSize(500,300); enterMessage=new Label("Enter Message"); to=new Label("To"); receivedMessages=new Label("Received Messages"); sentMessages=new Label("Sent Messages"); enterMessaget=new TextField(); receivedMessagest=new TextArea("",20,5,TextArea.SCROLLBARS_VERTICAL_ONLY); sentMessagest=new TextArea(); clientName=new Choice(); send=new Button("Send"); exit=new Button("Exit"); p1=new Panel(); p2=new Panel(); p3=new Panel(); p4=new Panel(); setLayout(new GridLayout(4,1)); p1.setLayout(null); p2.setLayout(new GridLayout(1,2)); p3.setLayout(new GridLayout(1,2)); p4.setLayout(new FlowLayout()); p1.add(enterMessage); enterMessage.setBounds(10,10,100,25); p1.add(enterMessaget); enterMessaget.setBounds(110,10,100,25); p1.add(to); to.setBounds(220,10,30,25); p1.add(clientName); clientName.setBounds(260,10,100,25); p1.add(send); send.setBounds(370,10,100,25); p2.add(receivedMessages); p2.add(receivedMessagest); p3.add(sentMessages); p3.add(sentMessagest); p4.add(exit); add(p1); add(p2); add(p3); add(p4); exit.addActionListener(this); send.addActionListener(this); try{ s=new Socket("localhost",2500); br=new BufferedReader(new InputStreamReader(s.getInputStream())); pw=new PrintWriter(s.getOutputStream(),true); setTitle(br.readLine()); }catch(Exception e){ System.out.println("Exception: Problem establishing connectionn"+e); } new ReceiveMsg().start(); } public static void main(String args[]){ new ChatClient(); } public void actionPerformed(ActionEvent ae){ if(ae.getSource()==send){ try{ pw.println("msg"); String client=clientName.getSelectedItem(); String msg=enterMessaget.getText(); pw.println(client); pw.println(msg); sentMessagest.append(client+" : "+msg+"n"); }catch(Exception e){ } } else if(ae.getSource()==exit){ try{ pw.println("shutdown"); }catch(Exception e){ } System.exit(0); } } class ReceiveMsg extends Thread{ public void run(){ while(true){ try{ String temp=br.readLine(); if(temp.equals("update")){ receiveUpdate(); } else if(temp.equals("msg")){ receiveMsg(); } }catch(Exception e){ } } } public void receiveMsg(){ try{ receivedMessagest.append(br.readLine()+"n"); }catch(Exception e){ } } public void receiveUpdate(){ try{ int n=Integer.parseInt(br.readLine()); clientName.removeAll(); for(int i=0;i<n;i++) clientName.add(br.readLine()); }catch(Exception e){ } } } class sendUpdate extends Thread{ public void run(){ while(true){ pw.println("alive"); try{ sleep(10000); }catch(Exception e){ } } } } }