




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
服务器端代码: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();/定义用户集合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(writer); 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) trythis.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);/将用户信息添加到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();System.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);/把传进来的用户信息跟说话内容分开System.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().getOutputStream();/建立用户自己的流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.println(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(:);/将传过来的用户名跟信息分隔开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.hasNext()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.flush();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=new Socket(,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 Runnablepublic 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();public 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=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.equals(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);/在我的频道显示进入聊天室信息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);/将刷新后所有用户信息添加到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.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 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(断开);TitledBorder 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.setLayout(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);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()&!jrb2.isSelected()&!jrb3.isSelected()JOptionPane.showMessageDialog(null,请选择性别!);elsejf.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.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 JPanel();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 JComboBox();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 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.setFixedCellHeight(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 GridLayout(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(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(false);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(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jsp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jsp2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jsp3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jsp3.setHorizontalScrollBarPolicy(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.println(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.println(456987);/发送下线标识pw.println(na+:离开聊天室);/发送下线信息pw.flush();jf.dispose();/关闭窗口catch(Exception ex) );public void actio
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度国际贸易结算与风险控制服务合同
- 2025版宿舍管理员绿色节能技术应用服务合同
- 2025版机场候机厅软装设计施工合同
- 2025年度吊装工程合同范本(含吊装设备维护与保养)
- 2025年度事业单位实习生实习合同
- 2025版绿色环保住宅区绿化施工与维护合同
- 2025版数据中心通风系统升级改造合同
- 2025年度男方外遇婚姻解除协议书范本
- 2025年度房产按揭贷款与装修贷优惠利率合同
- 2025年清洁服务人员安全培训及管理合同范本
- 《免除烦恼》课件
- 《非权力影响力》课件
- 2025年江西南昌市西湖城市建设投资发展集团有限公司招聘笔试参考题库附带答案详解
- 职业教育产教融合型数字化教材开发研究
- 文学传播学概论课件
- 第3单元主题活动三《创意玩具DIY》(课件)三年级上册综合实践活动
- 商务英语词汇大全
- 麻醉质量控制专家共识
- 反走私课件完整版本
- 2024-2025学年小学劳动一年级上册人教版《劳动教育》教学设计合集
- You Raise Me Up二部合唱简谱
评论
0/150
提交评论