📄 listener.java
字号:
import java.util.*;import java.io.*;import java.net.*;public class Listener extends Thread { private ServerSocket listener; private int port; String service; Set connections; private Service runService; private boolean stop_flag = false; int maxConn; private PrintWriter log = new PrintWriter(System.err, true); private ThreadGroup group; private int currentConnections = 0; Server server; Socket client = null; public Listener(Server server, int port, Service service, int maxConn, boolean bl) throws IOException { this.server = server; this.port = port; this.service = service.getClass().getName(); this.maxConn = maxConn; this.group = server.threadGroup; this.connections = server.connections; listener = new ServerSocket(this.port); if (bl == false) listener.setSoTimeout(600000); this.runService = service; if (!stop_flag) { for (int i=0;i<this.maxConn;i++) { ConnectionHandler currentThread = new ConnectionHandler(server,logStream); new Thread(this.group, currentThread, this.service+this.port+i).start(); this.logStream.realLog("向线程组" + group.toString() + "添加一个线程" + this.service+this.port+i); } } else throw new IllegalArgumentException("系统并发连接限制已经超过最大值!!"); } public Listener(Server server, int port, Service service, int maxConn) throws IOException { this(server, port, service, maxConn, false); } public void pleaseStop() { this.stop_flag = true; try { listener.close(); } catch (Exception e) { } this.interrupt(); } public void run() { while(!stop_flag) { try { client = listener.accept(); addConnection(client,runService); } catch (Exception e) {} } try { client.close(); } catch (IOException e) {} } private synchronized void addConnection(Socket s, Service service) { ConnectionHandler.requestToHandler(s, service); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -