📄 serverreceive.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.Adam;import java.net.*;import java.io.*;import javax.swing.*;/** * * @author Administrator */public class ServerReceive extends Thread{ServerSocket server; JTextField jTextField; JTextArea jTextArea; UserLinkList userlink; Node client; JComboBox jComboBox; public boolean isStop; public ServerReceive(JTextArea jTextArea,JTextField jTextField,JComboBox jComboBox,Node client,UserLinkList userlink) { this.jTextArea=jTextArea; this.jTextField=jTextField; this.jComboBox=jComboBox; this.client=client; this.userlink=userlink; isStop=false; } public void run(){ sendUserList(); while(!isStop || !server.isClosed()){ try{ String type=(String)client.input.readObject(); if(type.equalsIgnoreCase("聊天信息")){ String tosomebody=(String)client.input.readObject(); String action=(String)client.input.readObject(); String message=(String)client.input.readObject(); String status=(String)client.input.readObject();; String msg=client.username+ " "+action+ "对 "+ tosomebody+ " 说 : "+ message+ "\n"; if(status.equalsIgnoreCase("悄悄话")){ msg="[悄悄话]"+msg; } this.jTextArea.append(msg); if(tosomebody.equalsIgnoreCase("ALL")){ sendToAll(msg); }else{ try{ client.output.writeObject("聊天信息"); client.output.flush(); client.output.writeObject(msg); client.output.flush(); }catch(Exception e){ e.getMessage(); } Node node=userlink.findUser(tosomebody); if(node!=null){ node.output.writeObject("聊天信息"); node.output.flush(); node.output.writeObject(msg); node.output.flush(); } } }else if(type.equalsIgnoreCase("用户下线")){ Node node=userlink.findUser(client.username); userlink.DeleteUser(node); String msg="用户"+client.username+" 下线\n"; int count=userlink.getCount(); this.jComboBox.removeAllItems(); this.jComboBox.addItem("ALL"); int i=0; while(i<count){ node=userlink.findUser(i); if(node==null){ i++; continue; } this.jComboBox.addItem(node.username); i++; } this.jComboBox.setSelectedIndex(0); this.jTextArea.append(msg); this.jTextField.setText("在线用户" + userlink.getCount() + "人\n"); sendUserList(); sendToAll(msg); break; } }catch(Exception e){ e.getMessage(); } } } private void sendUserList() { String userlist=""; int count=userlink.getCount(); int i=0; while(i<count){ Node node=userlink.findUser(i); if(node==null){ i++; continue; } userlist=userlist+node.username; userlist=userlist+"\n"; i++; } i=0; while(i<count){ Node node=userlink.findUser(i); if(node==null){ i++; continue; } try{ node.output.writeObject("聊天信息"); node.output.flush(); node.output.writeObject(userlist); node.output.flush(); }catch(Exception e){ e.getMessage(); } i++; } } private void sendToAll(String msg) { int count=userlink.getCount(); int i=0; while(i<count){ Node node=userlink.findUser(i); if(node==null){ i++; continue; } try{ node.output.writeObject("聊天信息"); node.output.flush(); node.output.writeObject(msg); node.output.flush(); }catch(Exception e){ e.getMessage(); } i++; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -