1、JAVA聊天室源码/聊天室服务器import .*;import java.io.*;import java.util.*;import java.awt.event.*;import java.awt.*;import javax.swing.*;class Server extends JFrame private TextArea msgA = new TextArea();/服务器用来输入的文本域private TextArea area = new TextArea();/服务器显示消息的文本域private TextField portT = new TextField(8888)
2、;private java.awt.List list = new java.awt.List();/在线客户列表private ServerSocket server;/服务器private Socket client;/客户端private Map container = new HashMap();/HashMapvoid init() /设置几个面板/ Panel p1 = new Panel(); Panel p2 = new Panel(); Panel p3 = new Panel(); Panel p4 = new Panel(); Panel p5 = new Panel()
3、; Panel p6 = new Panel(); Panel p7 = new Panel(); Panel p8 = new Panel(); Panel p9 = new Panel(); Panel p10 = new Panel(); /设置两个主要的文本框 Label lb1 = new Label(服务器界面); lb1.setBackground(new Color(250,120,150); Label lb2 = new Label(port); BorderLayout border1 = new BorderLayout(); p1.setLayout(border1)
4、; p1.add(BorderLayout.CENTER, area); BorderLayout border2 = new BorderLayout(); p5.setLayout(border2); p5.add(BorderLayout.NORTH, p3); p5.add(BorderLayout.CENTER, msgA); BorderLayout border3 = new BorderLayout(); p7.setLayout(border3); p7.add(BorderLayout.CENTER, p1); p7.add(BorderLayout.SOUTH, p5);
5、 p7.setBackground(new Color(250,150,200); JButton startButton = new JButton(启动服务); JButton sendButton = new JButton(发送); JButton flushButton = new JButton(刷新列表); FlowLayout fl = new FlowLayout(FlowLayout.LEFT); p8.setLayout(fl); p8.add(lb2); p8.add(portT); p8.add(startButton); p8.add(sendButton); p8
6、.add(flushButton); p8.setBackground(new Color(250,120,150); BorderLayout border4 = new BorderLayout(); p9.setLayout(border4); p9.add(BorderLayout.CENTER, p7); p9.add(BorderLayout.SOUTH, p8); p10.setLayout(new BorderLayout(); Label lb3 = new Label(在线用户); lb3.setBackground(new Color(250,120,150); p10.
7、add(lb3,BorderLayout.NORTH); p10.add(list,BorderLayout.CENTER); area.setEditable(false); BorderLayout border = new BorderLayout(); this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent arg0) System.exit(0); ); this.setLayout(border); this.add(BorderLayout.NORTH, lb1); this
8、.add(BorderLayout.CENTER, p9); this.add(BorderLayout.EAST, p10); this.setTitle(聊天服务器 ); this.setBounds(100, 100, 600, 550); this.setVisible(true); /启动服务按钮监听器 startButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Thread t = new Start(); t.start(); );/启动按钮监听器-en
9、d /发送按钮监听器 sendButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String msg = msgA.getText(); if (msg != null & msg != ) /如果消息不为空 Calendar ca = new GregorianCalendar(); int hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE); int ss = ca.get(Cal
10、endar.SECOND); String time = new String(hh + : + mm + : + ss + ); broadcast(time + 系统消息: + msg);/那么广播这条系统消息 area.append(time + 系统消息: + msg + n); msgA.setText(); );/end /刷新按钮监听器 flushButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (list != null) list.remove
11、All();/清空在线用户列表,以便刷新 SetMap.Entry mySet = container.entrySet(); for (IteratorMap.Entry it = mySet.iterator(); it.hasNext();) /刷新列表 Map.Entry me = it.next(); String aClientName = me.getKey(); Socket aClient = me.getValue(); String aClientAddress = aClient.getInetAddress().getHostAddress(); list.add(a
12、ClientName + - + aClientAddress); /end of for /end of actionPerformed );/end msgA.addKeyListener(new KeyAdapter()/键盘监听器-按下Enter发送信息 public void keyPressed(KeyEvent e) int value = e.getKeyCode(); if(value = KeyEvent.VK_ENTER) String msg = msgA.getText(); Calendar ca = new GregorianCalendar(); int hh
13、= ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE); int ss = ca.get(Calendar.SECOND); String time = new String(hh + : + mm + : + ss + ); broadcast(time + 系统消息: + msg);/那么广播这条系统消息 area.append(time + 系统消息: + msg + n); msgA.setText(); );/end of init()class Start extends Thread /启动服务器的线程 p
14、ublic void run() try if(server != null & !server.isClosed() area.append(警告!服务器已在启动中!n); else server = new ServerSocket(Integer.parseInt(portT.getText();/启动服务器 area.append(new Date().toString() +n+ 恭喜!_+服务器启动成功! + nn); while(true) client = server.accept(); Thread sgS = new SgService(client);/为每位登录的客户
15、创建一个线程 sgS.start();/启动客户线程 catch (Exception e) System.out.println(cuowu 1); e.printStackTrace(); /end of run() /end of Start/处理单个客户的线程class SgService extends Thread/SgService-begin private Socket client = null;/客户套接字 private BufferedReader br = null;/输入流 private PrintStream ps = null; private int co
16、unter = 0;/记录是第几次接受用户的信息 private int mark = 0;/记录用户说不文明用语的次数 private String name = null; SgService(Socket client) /构造方法 this.client = client; public void run() /run()-begin try br = new BufferedReader(new InputStreamReader(client.getInputStream(); ps = new PrintStream(client.getOutputStream(); Strin
17、g msg = null; while (msg = br.readLine() != null) /无限循环,意味着可以和客户进行无限次交互 if(counter = 0) name = msg; container.put(name,client); list.add(name + - + client.getInetAddress().getHostAddress(); Calendar ca = new GregorianCalendar(); int hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE)
18、; int ss = ca.get(Calendar.SECOND); String time = hh + : + mm + : + ss + ; broadcast(time + name + 上线了!); area.append(time + name + 上线了!nn); counter +; else if(msg.equals(你是个笨蛋) mark = mark + 1; if(mark 3) Calendar ca = new GregorianCalendar(); int hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(
19、Calendar.MINUTE); int ss = ca.get(Calendar.SECOND); String time = new String(hh + : + mm + : + ss + ); singleCom(time +系统消息: 警告!请注意文明用语! ,client); /在服务器端显示这个用户说不文明语言的次数 area.append(time + 系统消息: + name +第 + mark+ 次使用了不文明用语,特此警告!); else/如果客户第三次说不文明的话,系统就把他踢出聊天室 Calendar ca = new GregorianCalendar(); i
20、nt hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE); int ss = ca.get(Calendar.SECOND); String time = new String(hh + : + mm + : + ss + ); if(mark = 3) singleCom(time + 系统消息: 由于您多次使用了不文明用语,经警告无效,现已被踢出聊天室! ,client);/告诉他本人 removeClient(name,client);/从系统列表中删除这个人的信息 /并向所有人广播这一消息 broadc
21、ast(time + 系统消息: + name + 由于多次使用了不文明用语,经警告无效,现已被踢出聊天室!); area.append(time + 系统消息: + name + 由于多次使用了不文明用语,经警告无效,现已被踢出聊天室!n); else Calendar ca = new GregorianCalendar(); int hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE); int ss = ca.get(Calendar.SECOND); String time = new String(h
22、h + : + mm + : + ss + ); area.append(time + name+说:+msg + nn);/将客户发送的消息显示在服务器的窗口中 broadcast(time + name+说:+msg+n);/同时向每个在线客户广播这则消息 /end of while catch (Exception ex) Calendar ca = new GregorianCalendar(); int hh = ca.get(Calendar.HOUR_OF_DAY); int mm = ca.get(Calendar.MINUTE); int ss = ca.get(Calend
23、ar.SECOND); String time = new String(hh + : + mm + : + ss + ); area.append(time + name + 下线了!n); broadcast(time + name + 下线了!n); list.remove(name + - + client.getInetAddress().getHostAddress(); container.remove(name); /run()-end/SgService-end void removeClient(String name,Socket client)/踢人 list.remo
24、ve(name + - + client.getInetAddress().getHostAddress(); container.remove(name);/广播void broadcast(String msg) /broadcast()-begin SetMap.Entry mySet = container.entrySet(); IteratorMap.Entry it = mySet.iterator(); String message = msg; while (it.hasNext() /如果列表中还有客户 Map.Entry me = it.next(); Socket te
25、mpClient = me.getValue(); singleCom(message, tempClient);/就把消息向发给这个客户 /broadcast()-end/向单个客户发送消息private void singleCom(String msg, Socket aClient) /singleCommunication-begin try PrintStream pw = new PrintStream(aClient.getOutputStream(); String message = msg; pw.println(msg); catch (Exception ex) ex
26、.printStackTrace(); /singleCommunication-endpublic static void main(String args) new Server().init(); /启动服务器窗口 System.out.println(服务器:);/end of Server/登录界面:import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import .*;class Login extends JFrame implements ActionListenerTe
27、xtField txtPort;TextField txtIP;TextField txtName;JButton btnLogin;JButton btnCancel;Socket toServer;BufferedReader in;PrintStream out;public Login() Panel p1=new Panel(); Panel p2=new Panel(); Panel p3=new Panel(); Panel p4=new Panel(); Panel p5=new Panel(); Label l1=new Label(port); Label l2=new L
28、abel(IP); Label l3=new Label(昵称); txtPort=new TextField(8888,15); txtIP=new TextField(192.168.55.1,15); txtName=new TextField(小敏,15); p2.add(l1); p2.add(txtPort); p3.add(l2); p3.add(txtIP); p4.add(l3); p4.add(txtName); GridLayout g=new GridLayout(3,1);/三行一列的网格布局管理器 p1.setLayout(g);/设置布局管理器 p1.add(p2
29、);/将组件添加到面板中 p1.add(p3); p1.add(p4); p1.setBackground(new Color(100,250,100);/更改面板颜色 btnLogin=new JButton(登陆); btnCancel=new JButton(取消); btnLogin.addActionListener(this);/添加监听器 btnCancel.addActionListener(this); BorderLayout border1=new BorderLayout();/边界布局管理器 this.setLayout(border1); p5.add(btnLogin); p5.add(btnCan
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1