局域网聊天系统.docx
- 文档编号:3969375
- 上传时间:2022-11-26
- 格式:DOCX
- 页数:51
- 大小:27.60KB
局域网聊天系统.docx
《局域网聊天系统.docx》由会员分享,可在线阅读,更多相关《局域网聊天系统.docx(51页珍藏版)》请在冰豆网上搜索。
局域网聊天系统
Ⅶ附录
7.1服务器端编码
7.1.1服务器程序
//Server.java
import.*;
importjava.io.*;
importjava.awt.List;
importjava.util.ArrayList;
importjava.util.Hashtable;
importjava.awt.Color;
importjavax.swing.JOptionPane;
publicclassServerextendsThread
{
ServerSocketserver=null;
ListonLineUsersList=null;
ListchatContentList=null;
ArrayListonLineUsers=newArrayList();
ArrayListchatContent=newArrayList();
booleanisClosed=false;
//在服务器端维护一个hashtable,用来存放各客户端的IP地址与用户昵称之间的对照关系映射。
Hashtableip2nickname=newHashtable();
ArrayListallService=newArrayList();
publicServer(intport,ListonLineUsersList,ListchatContentList)throwsIOException
{
this.server=newServerSocket(port);
this.onLineUsersList=onLineUsersList;
this.chatContentList=chatContentList;
this.start();
}
publicvoidrun()
{
while(!
isClosed)
{
try
{
Socketclient=null;
if(!
server.isClosed())
{
client=server.accept();
}
StringclientIP=client.getInetAddress().getHostAddress();
//判断该客户端已经连接的话,提示退出
if(ip2nickname.get(clientIP)!
=null)
{
ChatTookit.sendInfo(client,"已经有客户端从该ip地址连接服务器,本次连接将退出!
");
break;
}
//把新建连接的用户加入当前用户列表
onLineUsers.add(client);
//默认情况下,即在还没有收到用户自定义昵称的清空下,把用户的IP地址做为其昵称维护入ip2nickname
ip2nickname.put(clientIP,clientIP);
//在服务器端提示有新的客户连接
this.onLineUsersList.add(clientIP);
this.chatContentList.add(clientIP+"来了");
//对所有已连接的客户端,发送两条信息,1提示有新用户连接,2最新在线用户昵称列表
ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(ip2nickname));
ChatTookit.sendInfoToAll(onLineUsers,clientIP+"来了");
//针对每个client连接启动其特定服务线程
Serviceservice=newService(client,ip2nickname,onLineUsersList,chatContentList,onLineUsers,chatContent);
this.allService.add(service);;
}catch(IOExceptionex)
{
JOptionPane.showMessageDialog(null,"接收客户端连接时出现问题!
","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
publicvoiddestroy()
{
for(inti=0;i { Socketc=(Socket)onLineUsers.get(i); try { c.close(); }catch(IOExceptionex) { JOptionPane.showMessageDialog(null,"关闭客户端连接时候出现问题! ","提示",JOptionPane.INFORMATION_MESSAGE); } } try { this.isClosed=true; server.close(); }catch(IOExceptionex1) { JOptionPane.showMessageDialog(null,"关闭服务器时出现问题! ","提示",JOptionPane.INFORMATION_MESSAGE); } } publicvoidsend(Stringword)throwsIOException { if(word.startsWith("SPECIAL: ")) { word=word.substring(8); StringtoNickname=word.substring(0,word.indexOf("$SPECIAL$")); word=word.substring(word.indexOf("$SPECIAL$")+9); //根据昵称找到该客户端 Socketclient=ChatTookit.getSocketByIP(onLineUsers, ChatTookit.getIP(ip2nickname,toNickname)); ChatTookit.sendInfo(client,"系统管理员悄悄对你说: "+word); } else { chatContentList.add("系统管理员: "+word); ChatTookit.sendInfoToAll(onLineUsers,"系统管理员: "+word); } chatContentList.select(chatContentList.getItemCount()-1); } //强制更改用户的昵称 publicvoidchangeNickname(StringoldName,StringnewName) { //如果该昵称已经被别人使用则不进行更改 if(ip2nickname.contains(newName)) { chatContent.add("该昵称已经有人使用,所以不做任何更改"); this.chatContentList.select(this.chatContentList.getItemCount()-1); return; } //找到旧昵称所在的ip地址 Stringip=ChatTookit.getIP(ip2nickname,oldName); //用新昵称更新旧昵称 ip2nickname.put(ip,newName); //将负责该用户的Service里的昵称变量进行修改 Serviceservice=ChatTookit.getServiceByIP(allService,ip); service.changeNickname(newName); //刷新服务器端的显示 chatContentList.add("系统管理员将"+oldName+"的昵称改为"+newName); this.chatContentList.select(this.chatContentList.getItemCount()-1); ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname); //刷新各客户端的显示 try { ChatTookit.sendInfoToAll(onLineUsers,"系统管理员将"+oldName+"的昵称改为"+newName); ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(ip2nickname)); } catch(IOExceptionex) { JOptionPane.showMessageDialog(null,"发送信息时出错! ","提示",JOptionPane.INFORMATION_MESSAGE); } } publicvoidtickSocket(Stringnickname) { //先找到该昵称对应的socket,service,ip等信息 Stringip=ChatTookit.getIP(ip2nickname,nickname); Socketclient=ChatTookit.getSocketByIP(onLineUsers,ip); Serviceservice=ChatTookit.getServiceByIP(allService,ip); //从几个arraylist里把该客户端对应的信息都删除 this.allService.remove(service); this.onLineUsers.remove(client); this.ip2nickname.remove(ip); //关闭该客户端 try { //先给该客户端提供提示信息 ChatTookit.sendInfo(client,"你已经被系统管理员踢出聊天室"); client.close(); } catch(IOExceptionex) { JOptionPane.showMessageDialog(null,"关闭客户端"+ip+"时出错! ","提示",JOptionPane.INFORMATION_MESSAGE); } //刷新服务器端的显示 chatContentList.add("系统管理员将"+nickname+"踢出聊天室! "); this.chatContentList.select(this.chatContentList.getItemCount()-1); ChatTookit.updateOnLineUsersList(onLineUsersList,ip2nickname); //刷新各客户端的显示 try { ChatTookit.sendInfoToAll(onLineUsers,"系统管理员将"+nickname+"踢出聊天室! "); ChatTookit.sendInfoToAll(onLineUsers,ChatTookit.getAllNickname(ip2nickname)); } catch(IOExceptionex) { JOptionPane.showMessageDialog(null,"发送信息出错! ","提示",JOptionPane.INFORMATION_MESSAGE); } } } 7.1.2界面设计 //ServerFrame.java importjava.awt.BorderLayout; importjavax.swing.*; importcom.swtdesigner.SwingResourceManager; importjava.awt.*; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.io.*; publicclassServerFrameextendsJFrame { /** * */ privatestaticfinallongserialVersionUID=-2837827850953733407L; Serverserver=null; BorderLayoutborderLayout1=newBorderLayout(); JPaneljPanel1=newJPanel(); JPaneljPanel2=newJPanel(); JPaneljPanel3=newJPanel(); JPaneljPanel4=newJPanel(); JPaneljPanel5=newJPanel(); JPaneljPanel6=newJPanel(); JPaneljPanel7=newJPanel(); JButtonjButton1=newJButton(); JButtonjButton2=newJButton(); JButtonjButton3=newJButton(); JButtonjButton4=newJButton(); JButtonjButton5=newJButton(); JButtonjButton6=newJButton(); JButtonjButton7=newJButton(); JButtonjButton8=newJButton(); JLabeljLabel3=newJLabel(); Listlist1=newList(); Listlist2=newList(); BorderLayoutborderLayout2=newBorderLayout(); BorderLayoutborderLayout3=newBorderLayout(); BorderLayoutborderLayout4=newBorderLayout(); GridLayoutgridLayout1=newGridLayout(); GridLayoutgridLayout2=newGridLayout(); JTextAreajsend=newJTextArea(6,40); JCheckBoxjCheckBox1=newJCheckBox(); publicServerFrame(){ try{ jbInit(); setIconImage(SwingResourceManager.getImage(ServerFrame.class,"[Plugin]Icon/GameButton.ico")); }catch(Exceptionexception){ exception.printStackTrace(); } } privatevoidjbInit()throwsException { getContentPane().setLayout(borderLayout1); jsend.setText("注意言行"); jButton7.setText("发送"); jButton7.addActionListener(newServerFrame_jButton7_actionAdapter(this)); jButton1.setText("启动服务"); jButton1.addActionListener(newServerFrame_jButton1_actionAdapter(this)); jButton2.setText("关闭服务"); jButton2.addActionListener(newServerFrame_jButton2_actionAdapter(this)); jLabel3.setText("在线用户列表: "); jPanel2.setLayout(borderLayout2); jButton4.setText("断开连接"); jButton4.addActionListener(newServerFrame_jButton4_actionAdapter(this)); jPanel4.setLayout(borderLayout3); jButton6.setText("保存消息"); jButton6.addActionListener(newServerFrame_jButton6_actionAdapter(this)); jPanel6.setLayout(gridLayout1); gridLayout1.setColumns (1); gridLayout1.setRows(3); jPanel7.setLayout(gridLayout2); gridLayout2.setColumns (1); gridLayout2.setRows(5); jPanel5.setLayout(borderLayout4); jButton8.setText("清空消息"); jButton8.addActionListener(newServerFrame_jButton8_actionAdapter(this)); jCheckBox1.setSelected(true); jCheckBox1.setText("对所有人"); this.getContentPane().add(jPanel2,java.awt.BorderLayout.WEST); jPanel2.add(list1,java.awt.BorderLayout.CENTER); this.getContentPane().add(jPanel3,java.awt.BorderLayout.SOUTH); jPanel3.add(jsend); jPanel3.add(jButton7); this.getContentPane().add(jPanel4,java.awt.BorderLayout.EAST); jPanel6.add(jButton6,null); jPanel6.add(jButton8); jPanel7.add(jButton1); jPanel7.add(jButton2); jPanel7.add(jButton4); jPanel4.add(jPanel6,java.awt.BorderLayout.NORTH); jPanel4.add(jPanel7,java.awt.BorderLayout.SOUTH); jButton3.setText("修改昵称"); jButton3.addActionListener(newServerFrame_jButton3_actionAdapter(this)); jPanel7.add(jButton3); jButton5.setText("查看历史消息"); jButton5.addActionListener(newServerFrame_jButton5_actionAdapter(this)); jPanel6.add(jButton5,null); this.getContentPane().add(jPanel5,java.awt.BorderLayout.CENTER); jPanel5.add(list2,java.awt.BorderLayout.CENTER); this.getContentPane().add(jPanel1,java.awt.BorderLayout.NORTH); jPanel2.add(jLabel3,java.awt.BorderLayout.NORTH); jPanel2.add(jCheckBox1,java.awt.BorderLayout.SOUTH); this.setTitle("聊天室服务器端"); this.setSize(850,650); this.setLocation(100,100); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } publicstaticvoidmain(String[]args) { ServerFrameclientframe=newServerFrame(); } publicvoidjButton1_actionPerformed(ActionEvente) { try { server=newServer(9999,list1,list2); list2.add("服务器成功启动"); } catch(IOExceptionex) { JOptionPane.showMessageDialog(this,"启动服务器时出现错误,请检查并重新启动! ","提示",JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); } } publicvoidjButton2_actionPerformed(ActionEvente) { server.destroy(); list2.add("服务器成功关闭"); } publicvoidjButton8_actionPerformed(ActionEvente) { if(JOptionPane.showConfirmDialog(this,"确定要清空所有信息吗? ","清空信息",JOptionPane.YES_NO_OPTION)==0) { this.list2.removeAll(); } } publicvoidjButton7_actionPerformed(ActionEvente) { if(jsend.getText().length()>0) { try { if(jCheckBox1.isSelected()) { this.server.send(jsend.getText()); } else { if(list1.getSele
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 局域网 聊天 系统
![提示](https://static.bdocx.com/images/bang_tan.gif)