兰州交通大学博文学院毕业设计(论文)
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