教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 说明书 >

聊天室的实现与设计(7)

来源:网络收集 时间:2025-09-14
导读: 兰州交通大学博文学院毕业设计(论文) public UserInfoList() { } // add函数用于实现用户信息节点的添加 public void add(Node n) { } // del函数用于实现用户信息节点的删除 public void del(Node n) { } // get

兰州交通大学博文学院毕业设计(论文)

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字,全部文档内容请下载后查看。喜欢就下载吧 ……
聊天室的实现与设计(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/449201.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)