聊天室的实现与设计(7)
兰州交通大学博文学院毕业设计(论文)
public UserInfoList() { }
// add函数用于实现用户信息节点的添加 public void add(Node n) { }
// del函数用于实现用户信息节点的删除 public void del(Node n) { }
// getCount用于获取用户数 public int getCount() {
pointer = root;
while (pointer.next != null) { }
if (pointer.next == n) {// 判断当前节点是否为要删除的节点 }
pointer = pointer.next;
pointer.next = n.next; count--; break;
pointer = root;// 实例化游标
while (pointer.next != null) {// 判断是否用户链表尾部 }
pointer.next = n;// 向用户链表加入节点(用户添加) n.next = null; count++;
pointer = pointer.next;// 游标后移 root = new Node();// 创建链表根节点 root.next = null;
pointer = null;// 初始化游标 count = 0;// 用户数初始化
26
兰州交通大学博文学院毕业设计(论文)
}
// Find用于查找指定条件的用户 public Node Find(String UserName) { }
public Node Find(int index) {
if (count == 0 || index < 0)// 若用户数为0,或指定查询索引小于实际最if (count == 0)// 若用户数为0,返回空指针
return null; return count;
pointer = root;
while (pointer.next != null) { }
return null;
pointer = pointer.next;
if (pointer.userName.equals(UserName.trim()))
return pointer;
小值,返回空值 }
}
return null;
pointer = root; int i = 0;
while (i < index + 1) { }
return pointer;
if (pointer.next != null)
pointer = pointer.next;
else
return null;
i++;
27
兰州交通大学博文学院毕业设计(论文)
5.3.2 公聊功能实现
如果是群聊,则调用SendMessage中的SendMsgToAll发送到每一个客户端。期间会调用UserInfoList来获取所有用户。 public class SendMessageToAll { public SendMessageToAll() { }
public static void sendMsgToAll(UserInfoList uil,String M){ Node node;
int total = uil.getCount();// int index=0; while(index < total){
node = uil.Find(index);// if(node == null){ index++; continue; } try{
node.output.writeObject(\系统信息 ]\); node.output.flush(); node.output.writeObject(M); node.output.flush(); }catch(Exception e){ } index++; }
5.3.3私聊功能实现
如果是私聊,UserInfoList会根据用户名查找是哪个用户,再调用SendMessage方法将消息发送到指定客户端。 / Find用于查找指定条件的用户
public Node Find(String UserName) {
if (count == 0)// 若用户数为0,返回空指针
28
兰州交通大学博文学院毕业设计(论文)
}
Node node = userInfoList.Find((comboBox.getSelectedItem()
.toString()).trim());
return null; pointer = root;
while (pointer.next != null) { }
return null;
pointer = pointer.next;
if (pointer.userName.equals(UserName.trim()))
return pointer;
node.output.writeObject(\系统信息]\); node.output.flush();
node.output.writeObject((systemMsg.getText()).trim() + \); node.output.flush(); systemMsg.setText(\);
5.3.4 服务器端显示信息和发送信息功能实现
首先在客户Node类中定义了两个属性,分别是ObjectOutputStream和ObjectInputStream,这是java语言的输入输出流,应用于所有信息的传递,可以输入输出对象、数据、字符串等等。首先ServerListenerThread会捕获到客户端的请求,然后引用Node类并调用InputStream方法显示消息。
public class Node { String userName=null; Socket socket=null;
ObjectOutputStream output=null; ObjectInputStream input=null; Node next=null; }
public void run(){ while(!isStop){
29
兰州交通大学博文学院毕业设计(论文)
try{
String type=(String)client.input.readObject(); if(type.equalsIgnoreCase(\聊天信息]\)){
String toSomebody = (String)client.input.readObject(); String status = (String)client.input.readObject(); String action = (String)client.input.readObject(); String message = (String)client.input.readObject(); String msg = client.userName + action + \对\ +toSomebody +\说:\ +message +\;
if(status.equalsIgnoreCase(\悄悄话\)) msg=\悄悄话]\ + msg; textArea.append(msg);
if(toSomebody.equalsIgnoreCase(\大家\)){
SendMessageToAll.sendMsgToAll(userInfoList,msg); } else{
Node node = userInfoList.Find(toSomebody.trim()); if(node != null){
node.output.writeObject(\聊天信息]\); node.output.flush();
node.output.writeObject(msg); node.output.flush(); } }
}
5.3.5 服务器保存系统日志功能实现
//保存系统消息
public void saveInformation(Information inf) {
Session session=getSession();
30
…… 此处隐藏:1456字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [说明书]浅谈视觉传达设计中的色彩艺术
- [说明书]数据库原理-实验8-查询优化
- [说明书]临时用电施工组织设计
- [说明书]反假币考试试题第二套(人民币)
- [说明书]《建设工程监理规范》-2014.3.1实施
- [说明书]筋出槽,骨错缝病机诠释
- [说明书]百度预计来路对网站权重有多大影响
- [说明书]房屋面积测绘细则
- [说明书]计算机控制技术填空题汇总
- [说明书]乡镇卫生院(社区中心)死亡医学证明管
- [说明书]2013尾矿工实操试题
- [说明书]2015事业单位面试备考:贵州省事业单位
- [说明书](目录)2017-2022年半导体材料市场发
- [说明书]浙江省制造业发展“十三五”规划
- [说明书]干部选拔任用工作情况的自查报告
- [说明书]《发电厂电气部分》课后部分问答题整理
- [说明书]中国建立政府成本会计制度的思考
- [说明书]中国文化输出的现状及挑战-100分
- [说明书]通信原理实验指导(13-9)
- [说明书]HSF培训试卷(有害物质培训考试试题)
- 高中地理必修一:2.3《大气环境之六_气
- 新人教版八年级数学上期末总复习精品优
- WEB应用托管平台系统架构
- 《应用文写作》期末试卷
- 2017年秋九年级数学上册24.3正多边形和
- _空心村_形态特征与生成机理分析_以河
- 北师大版生物七年级下册12章2节感受器
- 社区卫生服务中心主任职责
- 广东省2014届广州二模材料作文“求道”
- 完整户口本英文翻译模板
- 2015年液化天然气LNG市场调研及发展趋
- 农村学校教育发展新
- 2014年注册税务师税法(Ⅱ)考试试题及答
- 东方大学俄语新版第5册第-7课课文翻译
- 路基路面工程课程设计1
- 219亩项目2011营销策略
- 第1篇 贸易术语-EXW、FCA、FAS
- 年会或大型活动防疫应急预案
- 改善提案(合理化建议)
- 初三英语第四单元ppt