1、服务器端代码:import .*;import java.io.*;import java.util.*;import java.util.concurrent.*;import javax.swing.*;public class Serverprivate static final int PORT=6666;G_Menu gm=new G_Menu();private ServerSocket server;public ArrayList list;public static String user;public static ArrayList list1=new ArrayList
2、();/定义用户集合public User uu;public Server(String user)this.user=user;public void getServer()list =new ArrayList();tryserver=new ServerSocket(PORT);System.out.println(“服务器启动,开始监听“);while(true)Socket client=server.accept();/接收客户端线程PrintWriter writer = new PrintWriter(client.getOutputStream();list.add(wri
3、ter); Thread t = new Thread(new Chat(client);t.start();catch(Exception ex)ex.printStackTrace();public static void main(String args)new Server(user).getServer();class Chat implements RunnableSocket socket;private BufferedReader br;private String msg;private String mssg=“;public Chat(Socket socket) tr
4、ythis.socket=socket;catch(Exception ex)ex.printStackTrace();public void run()trybr=new BufferedReader(new InputStreamReader(socket.getInputStream();while(msg=br.readLine()!=null)if(msg.equals(“1008611“)/匹配字符串 显示好友列表msg=br.readLine();String st=msg.split(“:“);/将用户信息跟消息分隔开uu=new User(st0,st1,socket);/将
5、用户信息添加到User 对象中list1.add(uu);/将对象添加到用户集合Iterator it=Server.list1.iterator();/遍历用户集合while(it.hasNext()User use=it.next();msg=use.getName()+“(“+use.getSex()+“):“;mssg+=msg;/将所有的用户信息连接成一个字符串sendMessage(“1008611“);/显示好友列表匹配标识sendMessage(mssg);/群发消息else if(msg.equals(“10010“)/显示说话消息msg=br.readLine();Syst
6、em.out.println(msg);sendMessage(“10010“);/显示说话信息匹配标识sendMessage(msg);else if(msg.equals(“10086“)/显示进入聊天室msg=br.readLine();System.out.println(msg);sendMessage(“10086“);/进入聊天室匹配标识sendMessage(msg);else if(msg.equals(“841163574“)/私聊msg=br.readLine();String rt=msg.split(“1072416535“);/把传进来的用户信息跟说话内容分开Sys
7、tem.out.println(rt1);/在服务器端显示说话内容String tg=rt0.split(“:“);/因为是私聊,传过来两个用户的用户信息,这句作用是再把两个用户信息分开Iterator iu=Server.list1.iterator();/遍历用户集合while(iu.hasNext()User se=iu.next();if(tg1.equals(se.getName()+“(“+se.getSex()+“)“)/如果传进来的用户信息跟集合中的用户信息吻合tryPrintWriter pwriter=new PrintWriter(se.getSock().getOutp
8、utStream();/建立用户自己的流pwriter.println(“841163574“);/匹配标识pwriter.println(rt1);/向单独用户发送消息pwriter.flush();System.out.println(rt1);catch(Exception ex)ex.printStackTrace();else if(tg0.equals(se.getName()/如果传进来的用户信息跟集合中的用户信息吻合tryPrintWriter pwr=new PrintWriter(se.getSock().getOutputStream();/建立用户自己的流pwr.pri
9、ntln(“841163574“);/匹配标识pwr.println(rt1);/向单独用户发送消息pwr.flush();System.out.println(rt1);catch(Exception ex)ex.printStackTrace();else if(msg.equals(“456987“)/下线msg=br.readLine();System.out.println(msg);/在服务端显示信息sendMessage(“456987“);/匹配字符串sendMessage(msg);/匹配完毕后群发消息String si=msg.split(“:“);/将传过来的用户名跟信息
10、分隔开Iterator at=Server.list1.iterator();/遍历用户集合while(at.hasNext()User sr=at.next();if(sr.getName().equals(si0)/如果传过来的用户名跟用户集合里的用户吻合list1.remove(sr);/将吻合的用户移除sr.getSock().close();/关闭此用户的 socketbreak;else if(msg.equals(“123654“)/刷新String mssge=“;Iterator iter=Server.list1.iterator();/遍历用户集合while(iter.h
11、asNext()User uus=iter.next();msg=uus.getName()+“(“+uus.getSex()+“):“;mssge+=msg;/将所有的用户信息连接成一个字符串sendMessage(“123654“);/发送刷新匹配标识sendMessage(mssge);/群发消息catch(IOException ex)ex.printStackTrace();public void sendMessage(String message)/群发消息方法tryfor(PrintWriter pw:list)/输出流集合pw.println(message);pw.flus
12、h();catch(Exception ex)ex.printStackTrace();客户端代码:import .*;import java.io.*;import java.util.*;import java.util.concurrent.*;public class Socket_one/客户端private static final int PORT=6666;/端口public static String user;public static Socket socket;public Socket_one(String user)this.user=user;trysocket=
13、new Socket(“127.0.0.1“,PORT);/建立 socket 连接System.out.println(“【“+user+“】欢迎来到聊天室!“);Thread tt=new Thread(new Recove(socket,user);/建立客户端线程tt.start();/启动线程catch(Exception ex)ex.printStackTrace();public static void main(String args) throws Exceptionnew Socket_one(user);class Recove implements Runnablepu
14、blic String user;private Socket socket;private BufferedReader keybord;public BufferedReader br;private PrintWriter pw;private String msg;G_Menu gm=new G_Menu();public Recove(Socket socket,String user) throws IOExceptiontrythis.socket=socket;this.user=user;catch(Exception ex)ex.printStackTrace();publ
15、ic void run()trybr=new BufferedReader(new InputStreamReader(socket.getInputStream();while(msg=br.readLine()!=null)String message=msg;if(message.equals(“1008611“)/匹配字符串 显示好友列表gm.listModel1.clear();/接收前清空好友列表gm.jcomb.removeAllItems();/清空 JComboxgm.jcomb.addItem(“所有人“);message=br.readLine();String str=
16、message.split(“:“);/将接收到的所有用户信息分隔开for(String ss:str)gm.listModel1.addElement(ss);/将所有用户信息添加到好友列表gm.jcomb.addItem(ss);/将所有用户信息添加到JComboxelse if(message.equals(“841163574“)/私聊message=br.readLine();System.out.println(“收到:“+message);/在服务器端显示私聊消息gm.jta2.append(message+“n“);/在我的频道显示私聊信息else if(message.equ
17、als(“10010“)/显示说话消息message=br.readLine();System.out.println(“收到:“+message);/在服务器端显示说话信息gm.jta1.append(message+“n“);/在公共频道显示说话信息gm.jta2.append(message+“n“);/在我的频道显示说话信息else if(message.equals(“10086“)/显示进入聊天室message=br.readLine();gm.jta1.append(message+“n“);/在公共频道显示进入聊天室信息gm.jta2.append(message+“n“);/
18、在我的频道显示进入聊天室信息else if(message.equals(“123654“)/刷新gm.listModel1.clear();/将好友列表清空gm.jcomb.removeAllItems();/将 JCombox 清空gm.jcomb.addItem(“所有人“);message=br.readLine();String sr=message.split(“:“);/将接收到的用户信息分隔开for(String sst:sr)gm.listModel1.addElement(sst);/将刷新后所有用户信息添加到好友列表gm.jcomb.addItem(sst);/将刷新后所
19、有用户信息添加到JComboxelse if(message.equals(“456987“)/下线message=br.readLine();gm.jta1.append(message+“n“);/在公共频道显示用户下线信息gm.jta2.append(message+“n“);/在我的频道显示用户下线信息catch(IOException ex)ex.printStackTrace();登陆界面代码:import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import javax.
20、swing.border.*;class Landen extends Frame implements ActionListenerJFrame jf=new JFrame(“聊天登陆“);JPanel jp1=new JPanel();JPanel jp2=new JPanel();JPanel jp3=new JPanel();JPanel jp4=new JPanel();JLabel jl1=new JLabel(“姓名:“);JLabel jl2=new JLabel(“地址:“);JLabel jl3=new JLabel(“端口:“);JRadioButton jrb1=new
21、 JRadioButton(“男生“);JRadioButton jrb2=new JRadioButton(“女生“);JRadioButton jrb3=new JRadioButton(“保密“);public JTextField jtf1=new JTextField(10);public JTextField jtf2=new JTextField(10);public JTextField jtf3=new JTextField(10);JButton jb1=new JButton(“连接 “);JButton jb2=new JButton(“断开 “);TitledBord
22、er tb=new TitledBorder(“);ButtonGroup gb=new ButtonGroup();public void init()/显示登录界面jb1.addActionListener(this);jb2.addActionListener(this);jp1.add(jl1);jp1.add(jtf1);jp1.add(jrb1);jp1.add(jrb2);jp1.add(jrb3);jp2.add(jl2);jp2.add(jtf2);jp2.add(jl3);jp2.add(jtf3);jp3.add(jb1);jp3.add(jb2);jp4.setLayo
23、ut(new GridLayout(3,1);jp4.add(jp1);jp4.add(jp2);jp4.add(jp3);jf.add(jp4);jtf2.setText(“localhost“);jtf3.setText(“6666“);gb.add(jrb1);gb.add(jrb2);gb.add(jrb3);jf.setLocation(200, 200);jf.setSize(350, 200);jf.setResizable(false);jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);jf.setVisible(true
24、);public void actionPerformed(ActionEvent event)/事件触发jb1.setText(“连接“);jb2.setText(“断开“);String s1=null;if(event.getActionCommand().equals(“断开“)System.exit(0);if(event.getActionCommand().equals(“连接“)if(jtf1.getText().equals(“)JOptionPane.showMessageDialog(null,“请输入用户名!“);else if(!jrb1.isSelected()el
25、sejf.setVisible(false);if(jrb1.isSelected()s1=“boy“;else if(jrb2.isSelected()s1=“girl“;else if(jrb3.isSelected()s1=“secret“;G_Menu gmu=new G_Menu();gmu.getMenu(jtf1.getText(),s1);gmu.sock();public class Loginpublic static void main(String args)new Landen().init();主界面代码:import java.awt.*;import java.
26、awt.event.*;import javax.swing.*;import javax.swing.border.*;import .*;import java.io.*;class G_Menu extends JFrame implements ActionListenerJFrame jf=new JFrame(“聊天室“);public Socket_one soc;public PrintWriter pw;public JPanel jp1=new JPanel();public JPanel jp2=new JPanel();public JPanel jp3=new JPa
27、nel();public JPanel jp4=new JPanel();public JPanel jp5=new JPanel();public JPanel jp6=new JPanel();public JPanel jp7=new JPanel();public static JTextArea jta1=new JTextArea(12,42);public static JTextArea jta2=new JTextArea(12,42);public JLabel jl1=new JLabel(“对“);public static JComboBox jcomb=new JC
28、omboBox();public JCheckBox jcb=new JCheckBox(“私聊“);public JTextField jtf=new JTextField(36);public JButton jb1=new JButton(“发送“);public JButton jb2=new JButton(“刷新“);public static DefaultListModel listModel1;public static JList lst1;public String na;public String se;public String message;public void
29、 getMenu(String name,String sex)/显示聊天界面jcomb.addItem(“所有人“);this.na=name;this.se=sex;jta1.setEditable(false);jta2.setEditable(false);listModel1= new DefaultListModel();lst1 = new JList(listModel1);lst1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);lst1.setVisibleRowCount(18);lst1.setFixedCel
30、lHeight(28);lst1.setFixedCellWidth(100);JScrollPane jsp1=new JScrollPane(jta1);JScrollPane jsp2=new JScrollPane(jta2);JScrollPane jsp3=new JScrollPane(lst1);jsp3.setBorder(new TitledBorder(“好友列表“);jsp1.setBorder(new TitledBorder(“主聊天频道“);jsp2.setBorder(new TitledBorder(“我的频道“);jp1.setLayout(new Grid
31、Layout(2,1);jp1.add(jsp1);jp1.add(jsp2);jp2.setLayout(new FlowLayout(FlowLayout.LEFT);jp2.add(jl1);jp2.add(jcomb);jp2.add(jcb);jp3.setLayout(new FlowLayout(FlowLayout.LEFT);jp3.add(jtf);jp3.add(jb1);jp4.setLayout(new GridLayout(2,1);jp4.add(jp2);jp4.add(jp3);jp5.setLayout(new BorderLayout();jp5.add(
32、jp1,BorderLayout.NORTH);jp5.add(jp4,BorderLayout.SOUTH);jp6.setLayout(new BorderLayout();jp6.add(jsp3,BorderLayout.NORTH);jp6.add(jb2,BorderLayout.SOUTH);jp7.setLayout(new FlowLayout(FlowLayout.LEFT);jp7.add(jp5);jp7.add(jp6);jf.add(jp7);jf.setLocation(200,200);jf.setSize(700,650);jf.setResizable(fa
33、lse);jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);jf.setVisible(true);jb1.addActionListener(this);jb2.addActionListener(this);jta1.setLineWrap(true);jta2.setLineWrap(true);jsp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jsp1.setHorizontalScrollBarPolicy(Scro
34、llPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jsp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jsp2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jsp3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jsp3.setHorizont
35、alScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jf.pack();public void sock()tryString user=na+“(“+se+“)“;/将用户信息保存成字符串形式soc=new Socket_one(user);/创建客户端对象pw=new PrintWriter(soc.socket.getOutputStream();/创建输出流pw.println(“1008611“);/发送好友列表标识pw.println(na+“:“+se);/发送用户信息pw.flush();pw.pr
36、intln(“10086“);/发送进入聊天室标识pw.println(“【“+na+“ 】“+“进入聊天室“);/发送进入聊天室信息pw.flush();catch(Exception ex)ex.printStackTrace();public G_Menu() /设置窗口关闭事件,如果点击窗口右上角叉号关闭,执行下边程序jf.addWindowListener( new WindowAdapter()public void windowClosing(WindowEvent e)try pw=new PrintWriter(soc.socket.getOutputStream();pw.
37、println(“456987“);/发送下线标识pw.println(na+“:离开聊天室“);/发送下线信息pw.flush();jf.dispose();/关闭窗口catch(Exception ex) );public void actionPerformed(ActionEvent event)/事件触发jb1.setText(“发送“);jb2.setText(“刷新“);trypw=new PrintWriter(soc.socket.getOutputStream();if(event.getActionCommand().equals(“发送“)/ 点击发送触发if(!jtf
38、.getText().equals(“)if(jcb.isSelected()String name1=(String)jcomb.getSelectedItem();message=“悄悄话“+na+“(“+se+“)“+“对“+name1+“ 说:“+jtf.getText();pw.println(“841163574“);/发送私聊标识pw.println(na+“:“+name1+“1072416535“+message);/发送私聊信息pw.flush();elsepw.println(“10010“);/发送聊天标识pw.println(na+“说:“+jtf.getText()
39、;/发送聊天信息pw.flush();else if(event.getActionCommand().equals(“刷新“)/点击刷新触发pw=new PrintWriter(soc.socket.getOutputStream();pw.println(“123654“);/发送刷新标识pw.flush();catch(Exception ex)ex.printStackTrace();jtf.setText(“);/清空输入栏信息jtf.requestFocus();/输入焦点实体类代码:import java.util.*;import .*;import java.io.*;cla
40、ss Userprivate String name;/用户姓名private String sex;/用户性别private Socket sock;/用户自己的 socketpublic User(String name,String sex,Socket sock)setName(name);setSex(sex);setSock(sock);public String getName()return name;public void setName(String name)this.name=name;public String getSex()return sex;public void setSex(String sex)this.sex=sex;public Socket getSock()return sock;public void setSock(Socket sock)this.sock=sock;使用说明:1、先将所有的类都编译一下2、先运行服务器端代码3、再运行登录界面代码