import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.net.*;
import java.sql.*;
class Client extends JFrame implements ActionListener
{
String name;
String msg;
String target="";
int index;
Socket s;
InputStreamReader isr;
BufferedReader br;
PrintWriter pw;
/*login frame data*/
JFrame loginframe;
JButton login,cancel1;
JTextField loginid;
JLabel loginlb;
Icon canimg,logimg;
/*chat frame data*/
JFrame chatframe;
JTextArea text;
JTextField datatosend;
JButton send;
JButton clear;
JScrollPane jp,jp1;
JLabel received,wannasend;
JComboBox cb;
JButton logout;
JButton sendtoall;
Icon sendicon,logouticon,clearicon,sendtoallicon;
Client()
{
try
{
s=new Socket("localhost",8080);
isr=new InputStreamReader(s.getInputStream());
br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream());
Readdata m=new Readdata(isr);
Thread t1=new Thread(m);
t1.start();
}
catch(Exception e){
System.out.println(""+e);
}
/*chat frame begin*/
chatframe=new JFrame();
text=new JTextArea();
datatosend=new JTextField();
text.setEditable(false);
sendicon=new ImageIcon("send.png");
logouticon=new ImageIcon("logout.png");
clearicon=new ImageIcon("clear.png");
sendtoallicon=new ImageIcon("sendtoall.gif");
send=new JButton(sendicon);
clear=new JButton(clearicon);
logout=new JButton(logouticon);
sendtoall=new JButton(sendtoallicon);
jp=new JScrollPane(text);
jp1=new JScrollPane(datatosend);
received=new JLabel("Received msg:");
wannasend=new JLabel("Msg to send:");
received.setBounds(10,5,90,30);
jp.setBounds(10,30,250,100);
wannasend.setBounds(10,130,90,30);
jp1.setBounds(10,160,250,80);
send.setBounds(15,250,72,28);
clear.setBounds(120,250,72,28);
logout.setBounds(250,250,72,28);
sendtoall.setBounds(370,250,80,28);
chatframe.setLayout(null);
chatframe.add(received);
chatframe.add(jp);
chatframe.add(wannasend);
chatframe.add(jp1);
chatframe.add(send);
chatframe.add(clear);
chatframe.add(sendtoall);
//chatframe.setVisible(true);
chatframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
chatframe.addWindowListener(new appCloseHandler());
cb=new JComboBox();
cb.setBounds(280,30,170,30);
chatframe.add(cb);
cb.addActionListener(new comboboxhandler());
chatframe.add(logout);
chatframe.setSize(500,350);
clear.addActionListener(this);
logout.addActionListener(this);
send.addActionListener(this);
sendtoall.addActionListener(this);
/*chatframe ends*/
/*login frame begin*/
loginframe=new JFrame("login@milan.com");
loginframe.setLocation(400,300);
loginid=new JTextField();
loginlb=new JLabel("Login-Id:");
canimg=new ImageIcon("cancel.png");
logimg=new ImageIcon("login.png");
login=new JButton(logimg);
cancel1=new JButton(canimg);
loginframe.setLayout(null);
loginframe.setVisible(true);
loginframe.setSize(230,150);
loginlb.setBounds(10,5,80,30);
loginid.setBounds(80,10,130,20);
login.setBounds(15,50,72,28);
cancel1.setBounds(130,50,72,28);
loginframe.add(login);
loginframe.add(loginid);
loginframe.add(loginlb);
loginframe.add(cancel1);
login.addActionListener(this);
cancel1.addActionListener(this);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*loginframe ends*/
}
public String getname()
{
return(name);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==clear)
{
datatosend.setText("");
}
if(e.getSource()==cancel1)
{
loginid.setText("");
//System.exit(0);
}
if(e.getSource()==login)
{
name=loginid.getText();
chatframe.setTitle(name);
loginframe.setVisible(false);
chatframe.setVisible(true);
String st=name+"123logged";
try{
pw.flush();
pw.println(st);
pw.flush();
}
catch(Exception e1){System.out.println("exception1:"+e1);}
}
if(e.getSource()==send)
{
String str="::"+name+"::"+target+"::"+datatosend.getText()+"::"+index;
datatosend.setText("");
try
{
pw.flush();
pw.println(str);
pw.flush();
}
catch(Exception ew)
{
System.out.println("send error");
}
}
if(e.getSource()==sendtoall)
{
String str="@"+name+"@"+datatosend.getText();
datatosend.setText("");
try
{
pw.flush();
pw.println(str);
pw.flush();
}
catch(Exception ew)
{
System.out.println("send error");
}
}
if(e.getSource()==logout)
{
logout();
}
}
public void logout()
{
System.out.println("index is "+getNameIndex(name));
String str="~"+name+"~"+"logout";
try
{
pw.flush();
pw.println(str);
pw.flush();
}
catch(Exception e5)
{
System.out.println("logout errot");
}
System.exit(0);
}
public int getNameIndex(String name)
{
int index=0;
for(int j=0;j
String temp=(String)cb.getItemAt(j);
if(temp.equals(name))
index=j;
}
return index;
}
public static void main(String... s)
{
new Client();
}
class Readdata implements Runnable
{
InputStreamReader isr;
BufferedReader br;
Readdata(InputStreamReader isr)
{
this.isr=isr;
br=new BufferedReader(isr);
}
public void run()
{
String s1="";
do
{
try
{
s1=br.readLine();
if(s1.startsWith("*"))
{
System.out.println("data recieved"+s1);
String[] clientname=s1.split("123");
cb.addItem(clientname[1]);
}
else if(s1.startsWith("#"))
{
String[] temp=s1.split("#");
//System.out.println("length is "+temp.length);
for(int i=0;i
int removeindex=Integer.parseInt(""+getNameIndex(temp[2]));
cb.removeItemAt(removeindex);
}
else{
text.append(s1+"\n");
text.setCaretPosition(text.getText().length());
}
}
catch(Exception e)
{
System.out.println("exception2:"+e);
}
}while(s1!=null);
}
}
/*class sendListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}*/
class comboboxhandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
target=(String)cb.getSelectedItem();
index=(int)cb.getSelectedIndex();
}
}
class appCloseHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
logout();
}
}
}//class end
//Server.java
import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
ArrayList
ArrayList
ServerSocket ss;
Socket s;
String name;
String cl="*";
String source="";
String target="";
String datatosend="";
int index;
Server()
{
try
{
al=new ArrayList
namelist=new ArrayList
ss=new ServerSocket(8080);
System.out.println("Server Started");
while(true)
{
s=ss.accept();
al.add(s);
Runnable r=new serverread(s,al);
Thread t=new Thread(r);
t.start();
}
}
catch(Exception exc)
{
System.out.println("exception:"+exc);
}
}//constructor ends
public static void main(String... s)
{
new Server();
}
class serverread implements Runnable
{
Socket s;
ArrayList me;
serverread(Socket s,ArrayList me)
{
this.s=s;
this.me=me;
}
public void run()
{
String data="";
try
{
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
do{
data=new String(br.readLine());
if(data.startsWith("::"))
{
System.out.println("data to send is "+data);
String token[]=data.split("::");
source=token[1];
target=token[2];
datatosend=token[3];
index=Integer.parseInt(""+getNameIndex(target));
sendtotarget(source,target,datatosend,index);
}
else if(data.endsWith("logged"))
{
String[] tokens=data.split("123");
name=tokens[0];
namelist.add(name);
updateotherclient(name);
}
else if(data.startsWith("@"))
{
System.out.println("data starts with @");
String tok[]=data.split("@");
for(int i=0;i
source=tok[1];
datatosend=tok[2];
int index=getNameIndex(source);
sendexceptme(index,datatosend,source);
}
else
{
System.out.println("ends with logout");
System.out.println(data);
String[] temp=data.split("~");
for(int j=0;j
String removeclientname=temp[1];
int removeindex=getNameIndex(removeclientname);
al.remove(removeindex);
namelist.remove(removeindex);
updateeveryclient("#"+"x"+"#"+removeclientname+"#");
}
}while(data!=null);
}
catch(Exception es)
{
System.out.println("exception:"+es);
}
}
public int getNameIndex(String name)
{
int index=0;
for(int j=0;j
String temp=namelist.get(j);
if(temp.equals(name))
index=j;
}
return index;
}
public void updateotherclient(String name)
{
try
{
for(int i=0;i
Socket sc=al.get(i);
PrintWriter pw=new PrintWriter(sc.getOutputStream());
pw.flush();
pw.println("*"+"123"+name+"123"+"*");
pw.flush();
}
for(int j=0;j
Socket sc=al.get(namelist.size()-1);
PrintWriter pw=new PrintWriter(sc.getOutputStream());
pw.flush();
pw.println("*"+"123"+namelist.get(j)+"123"+"*");
pw.flush();
}
}
catch(Exception er)
{
System.out.println("error:"+er);
}
}
public void sendtotarget(String source,String target,String datatosend,int index)throws Exception
{
String lastdata=source+":"+datatosend;
Socket sc=al.get(index);
PrintWriter pw=new PrintWriter(sc.getOutputStream());
pw.flush();
pw.println(lastdata);
pw.flush();
}
public void sendexceptme(int index,String datatosend,String src)
{
String data=src+":"+datatosend;
try
{
for(int i=0;i
if(i!=index)
{
Socket sc=al.get(i);
PrintWriter pw=new PrintWriter(sc.getOutputStream());
pw.flush();
pw.println(data);
pw.flush();
}
}
}
catch(Exception er)
{
System.out.println("error:"+er);
}
}
public void updateeveryclient(String cl)throws Exception
{
try
{
for(int i=0;i
Socket sc=al.get(i);
PrintWriter pw=new PrintWriter(sc.getOutputStream());
pw.flush();
pw.println(cl);
pw.flush();
}
}
catch(Exception er)
{
System.out.println("error:"+er);
}
}
}
}
//Please use different names to login.........
Download
0 comments:
Post a Comment