利用JAVA实现简单聊天室1.设计思路Java是一种简单的,面向对象的,分布式的,解释的,键壮的,安全的,结构中立的,可移植的,性能很优异的,多线程的,动态的语言。
而且,Java 很小,整个解释器只需215K的RAM。
因此运用JAVA程序编写聊天室,实现简单聊天功能。
程序实现了聊天室的基本功能,其中有:(1)启动服务器:实现网络的连接,为注册进入聊天室做准备。
(2)注册登陆界面:填写基本信息如姓名等,可以供多人进入实现多人聊天功能。
(3)发送信息:为用户发送信息提供平台。
(4)离开界面:使用户退出聊天室。
(5)关闭服务器:断开与网络的连接,彻底退出聊天室。
2.设计方法在设计简单聊天室时,需要编写5个Java源文件:、、、、。
3 程序功能图及程序相关说明(1)主功能框图(2) 聊天室基本功能表4.程序代码是说明程序中引入的包:package Chat; import .*;import .*;import .*;import .*;import .*;(1)服务器端代码中用户自定义类:类名:Server作用:服务器启动继承的接口名:ActionListenerpublic class Server implements ActionListener{定义的对象:count 计结果与分析5个Java源文件:、、、、。
分别编译这5个Java源文件,或运行“javac *.java”命令,编译全部的源文件,然后运行连接服务器的主类,即运行Server类。
连接完毕后在运行注册的主函数类,即运行LogIn类。
首先运行启动服务器的文件,运行结果如下:图1服务器启动成功结果分析:在进入聊天室之前必须先启动服务器,实现服务器端的连接为用户的登录打开“窗口”。
其次运行用户注册登录文件,运行结果如下:图2用户登陆界面结果分析:添加用户信息,输入用户姓名,为用户进入聊天室提供关口。
并且利用以下语句实现如图3的界面 JLabel title=new JLabel("免注册版"); JLabel name=new JLabel("输入姓名");JButton send=new JButton("进入");添加注册信息后点击“进入”按钮,结果如图3图3显示用户已登录结果分析:当服务器启动用户完成信息注册时,在服务期端的界面中会显示当前在线的人员,并且提示客户已连接到服务器,可以进行聊天行为。
图4显示聊天内容结果分析:多个用户进入聊天室之后,用户可以在发送信息的文本框里填写自己的内容,点击“发送信息”按钮,信息会出现在上方的区域内,在界面的右方还可以显示在线人员的姓名,另在线用户可以知道有谁在聊天室内。
聊天结束点击“离开”按钮,结果如图5所示:图5离开聊天室点击服务器中的“停止”按钮,结果如图6.图7.所示:图6 点击“停止”按钮图7终止与服务器端连接结果分析:用户聊天结束点击“离开”按钮之后会在服务器端的界面上自动显示该用户已离开,提时、示服务器执行相关行为。
离开界面:使用户退出聊天室;关闭服务器:断开与网络的连接,彻底退出聊天室。
6.语句实现(1)package Chat;import .*;import .*;import .*;import .*;import .*;public class Server implements ActionListener{int count=0;Vector soconly=new Vector();HashMap sockets=new HashMap();HashMap socket_thread=new HashMap();ServerSocket serversocket;JFrame frame;JTextArea area=new JTextArea();JPanel panel=new JPanel();JButton start=new JButton("启动");JButton stop=new JButton("停止");public Server() {(start);(stop);(panel,;(area,;();center();;(true);(this);(this);"new");}public void center(){Toolkit toolkit=();Dimension e=();int x=2-(int)().getWidth()/2);int y=2-(int)().getHeight()/2);(x,y);}public void actionPerformed(ActionEvent e){JButton btn=(JButton)();if(btn==start){serverRun serverrun=new serverRun();"gogo");();}if(btn==stop){try{Iterator i = ();while ()) {Socket s = (Socket) ();ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());Objecting bj=new Objecting();("系统消息-----服务器马上关闭!!!!!!请马上关闭客户端"); }count++;if(count==2){(0);}(frame,"已通知客户端,请再次点击关闭按钮关闭");}catch(Exception ex){();}}}class serverRun extends Thread{public void run(){try{"开始SERVERRUN");serversocket=new ServerSocket(55555);("服务器启动\n");(false);while(true){Socket socket=();"连接成功");BufferedReader br=new BufferedReader(new InputStreamReader())); String name=();("服务器接收用户"+name+"的连接\n");(socket);Iterator i=();while()){Socket s=(Socket)();;import .*;import class LogIn implements ActionListener{JFrame frame;JLabel title=new JLabel("免注册版");JPanel banner=new JPanel();JTextField text=new JTextField(10);JPanel body=new JPanel();JPanel buttom=new JPanel();JLabel name=new JLabel("输入姓名");JButton send=new JButton("进入");public LogIn() {frame=new JFrame("注册");;(title);(name);(text);(send);(banner,;(body,;(buttom,;();center();(true);(this); }public void center(){Toolkit toolkit=();Dimension e=();int x=2-(int)().getWidth()/2);int y=2-(int)().getHeight()/2);(x,y); }public void actionPerformed(ActionEvent e){if().equals("")){(frame,"姓名不能为空");return;}else{new Client());(); } }public static void main(String args[]){new LogIn(); } }(4)package Chat;import .*;import .*;import .*;import .*;public class ClientUser extends WindowAdapter implements ActionListener,Runnable{ public JFrame jf;public JTextField jt;public JTextField name;public JButton connect;public JScrollPane jsp;public JTextArea jta;public JButton jb;public JLabel l;Socket soc;InputStream soc_in;OutputStream soc_out;BufferedReader Bsoc_in;PrintWriter Psoc_out;public ClientUser(){jf=new JFrame("Client Chat Room");jta=new JTextArea(10,30);(false);jsp=new JScrollPane(jta,,;jt=new JTextField(30);name=new JTextField(5);l=new JLabel("Please in put your name");connect=new JButton("Connect");(this);(this);jb=new JButton("Send The Message");(this);JPanel jp=new JPanel();JPanel jp2=new JPanel();(new FlowLayout());(jt);(jb);(l);(name);(connect);Container c=();(new GridBagLayout());GridBagConstraints gbc=new GridBagConstraints();=0;=0;(jp2,gbc);=0;=1;(jsp,gbc);=2;(jp,gbc);();(true); }public static void main(String[] args) {ClientUser cuser=new ClientUser(); }public void windowClosing(WindowEvent e){(0);}public void actionPerformed(ActionEvent e){if ()==connect){if().equals("")){new JOptionPane().showMessageDialog(null,"Please in Put your name First!");}else{try{soc=new Socket("localhost",55555);Psoc_out=new PrintWriter(),true);Bsoc_in=new BufferedReader(new InputStreamReader()));());}catch(Exception e1){();}(false);new Thread (this).start();} }if()==jt||()==jb){()+":"+()+"\n");()+":"+());("");} }public void run(){String text;while(true){try{text=();(text+"\n");}catch(Exception e){();}} }(5)package Chat;import .*;import .*;import .*;import .*;import class Client implements ActionListener{ ClientSocket cs;String name;Socket socket;JFrame frame;JTextArea area=new JTextArea(30,30);JTextField text=new JTextField(30);JButton send=new JButton("发送消息");JButton exit=new JButton("离开");JPanel banner=new JPanel();JPanel panel=new JPanel();List list=new List(35);JPanel right=new JPanel();JLabel welcome=new JLabel();public Client(String name2) {=name2;(name+"----欢迎来到聊天室");(false);frame=new JFrame("聊天室");;(false);(welcome);(banner,;(text);(send);(exit);(panel,;(area,;(new JScrollPane(list));(right,;();center();(true);connect();(this);(this);(new ActionListener(){public void actionPerformed(ActionEvent e){String to=();String content=(frame,"输入向"+name+"发送的私聊信息","私聊",; if(content==null){return;}else{Objecting o=new Objecting(to,"---私聊信息--"+name+":"+content); try{ObjectOutputStream out = new ObjectOutputStream());(o);(frame,to+"收到消息");}catch(Exception e2){();}}}});(new ActionListener(){public void actionPerformed(ActionEvent e){if().equals("")){(frame,"消息为空");return;}else{Objecting o=new Objecting("all",name+":"+())try{ObjectOutputStreamout=new ObjectOutputStream());(o);("");}catch(Exception e2){();}} } }); }public void connect(){try{socket = new Socket("localhost",55555);cs=new ClientSocket();();}catch(Exception e){();} }public void center(){Toolkit toolkit=();Dimension e=();int x=2-(int)().getWidth()/2);int y=2-(int)().getHeight()/2);(x,y);}class ClientSocket extends Thread{ObjectInputStream in;ObjectOutputStream out;PrintWriter pw;public void run(){try{pw = new PrintWriter(),true);(name);}catch(Exception e){();}while(true){try{in = new ObjectInputStream());Object o=();if(o instanceof String){()+"\n"); }if(o instanceof Vector){"列表操作");Vector v=(Vector)o;();Object []obj=();for(int i=0;i<;i++){((String)obj[i]); } }}catch(Exception e){();} } } }public void actionPerformed(ActionEvent e){JButton btn=(JButton)();if(btn==send){if().equals("")){(frame,"消息为空");return;}else{Objecting o=new Objecting("all",name+":"+());try{ObjectOutputStream out = new ObjectOutputStream());(o);("");}catch(Exception e2){();}}}if(btn==exit){Objecting o=new Objecting("exit",name);try{ObjectOutputStream out = new ObjectOutputStream());(o);();();}catch(Exception x){();}(0);} } }(5)package Chat;import .*;import .*;import .*;import .*;import .*;public class ServerHost extends WindowAdapter implements ActionListener,Runnable{ public JFrame jf;public JTextField jt;public JScrollPane jsp;public static JTextArea jta;public JButton jb;HashMap hm=new HashMap();JLabel inof;JTextField name;JCheckBox check;OutputStream soc_out;Vector Thread_vector=new Vector();public ServerHost(){jf=new JFrame("Server Chat Room");jta=new JTextArea(10,30);(false);jsp=new JScrollPane(jta,,;jt=new JTextField(30);(this);jb=new JButton("Send The Message");(this);check=new JCheckBox("Send to all");(true);inof=new JLabel("write the name you want to send");name=new JTextField(5);(false);JPanel jp=new JPanel();(new FlowLayout());(inof);(name);(jt);(jb);(check);Container c=();(new GridBagLayout());GridBagConstraints gbc=new GridBagConstraints();=0;=0;(jsp,gbc);=1;(jp,gbc);();(true);}public void windowClosing(WindowEvent e){(0); }public static void main(String[] args) {ServerHost sh=new ServerHost();new Thread(sh).start(); }public void run(){ServerSocket ssc=null;try{ssc=new ServerSocket(55555);}catch(Exception e){"1");}while(true){try{Socket soc=();BufferedReaderBsoc_in=new BufferedReader(new InputStreamReader()));String s=();(soc,s);ServerThread st=new ServerThread(soc); (soc);new Thread(st).start();}catch(Exception e){"2");}} }public void actionPerformed(ActionEvent e){boolean find=false;String temp="";try{if()){for(int i=0;i<();i++){Socket s=(Socket)(i));PrintWriterPsoc_out=newPrintWriter( (),true);temp="Server:"+()+"\n";("Server:"+());(); }(temp);(""); }else{"aaaaaaaa");Set shm=();Iterator it=();while()){me=());String Sstr=(String)());if())){Socket Smsoc=(Socket)());PrintWriterPsoc_out=newPrintWriter( (),true);()+());find=true; } }if(find==false){new JOptionPane().showMessageDialog(null,"Can not find the Clientuser");} }}catch(Exception e3){();}}}class ServerThread implements Runnable{Socket socket;InputStream soc_in;BufferedReader Bsoc_in;ServerThread(Socket socket){=socket;}public void run(){try{soc_in=();Bsoc_in=new BufferedReader(new InputStreamReader(soc_in)); String s=();while(true){if(s!=null)"\n");s=();}}catch(Exception e){"3");}}}。