⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverthreadmanager.java.svn-base

📁 多线程加密聊天室,支持多线程客户端链接服务器端
💻 SVN-BASE
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package safechatserver;import mytools.AES;import java.io.*;import java.net.*;import java.util.ArrayList;import javax.swing.*;/** *服务器管理线程,用于管理与客户端的会话 * @author DJ尐舞 */public class ServerThreadManager extends Thread {    ArrayList dialogserveArray = new ArrayList();    private ServerSocket server;    private boolean[] listening = new boolean[1];    private DefaultListModel StateListM;    private DefaultListModel UserListM;    AES serverAES=new AES();    TbModelTool tmt;    public ServerThreadManager(ServerSocket server, boolean[] listening, DefaultListModel StateListM,DefaultListModel UserListM) {        this.server = server;        this.listening = listening;        this.StateListM = StateListM;        this.UserListM=UserListM;        tmt=new TbModelTool();//数据库工具    }    public void publishInfo(String info) throws IOException{        for (int i = 0; i < dialogserveArray.size(); i++) {            ServeClientThread theDialog = (ServeClientThread) dialogserveArray.get(i);            if (theDialog.isAlive()) {                theDialog.sendInfo(info);            }        }    }        @Override    public void run() {        while (listening[0] && Thread.currentThread() == this) {            try {                Thread.sleep(300);            } catch (Exception e) {            }            try {                server = new ServerSocket(5000, 10);            } catch (IOException ie) {                StateListM.addElement("无法在5000端口监听");                ie.printStackTrace();                return;            }            StateListM.addElement("服务器监听中");            try {                while (true) {                    Socket client = server.accept();                    Thread dialogThread;                    dialogThread = new ServeClientThread(client, listening,serverAES,tmt, StateListM,UserListM);                    dialogserveArray.add(dialogThread);                    dialogThread.start();                }            } catch (IOException ie) {                ie.printStackTrace();            }        }        System.out.println("线程退出");    }    //关闭所有对话,当服务器暂停监听时    private void closeAlldialog() {        for (int i = 0; i < dialogserveArray.size(); i++) {            ServeClientThread theDialog = (ServeClientThread) dialogserveArray.get(i);            if (theDialog.isAlive()) {                theDialog.endSocket();            }        }    }    public void stopListen() {        closeAlldialog();        try {            server.close();        } catch (IOException ex) {        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -