📄 listener.java
字号:
package proxy;
import java.net.Socket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.io.InterruptedIOException;
/**
* 监听处理类
**/
public class Listener extends Thread {
ProxyServer ProxyServerHandle = null;
ServerSocket listen_socket;
int port;
Service service;
volatile boolean stop = false; //是否请求停止
/**
* 创建线程并加入到线程组中
**/
public Listener(ProxyServer ProxyServer, ThreadGroup group, int port, Service service)
throws IOException
{
super(group, "Listener:" + port);
this.ProxyServerHandle = ProxyServer;
listen_socket = new ServerSocket(port);
//服务器从客户端读取数据超时时间
listen_socket.setSoTimeout(ProxyServer.SERVER_TIME_OUT);
this.port = port;
this.service = service;
}
/**
* 停止服务
***/
public void pleaseStop() {
this.stop = true;
this.interrupt();
try {
listen_socket.close();
}
catch (IOException e) {}
}
public void run() {
while (!this.stop) {
try {
Socket client = listen_socket.accept();
//设置超时间,解决SOCKET阻塞
client.setSoTimeout(ProxyServer.SERVER_TIME_OUT);
this.ProxyServerHandle.addConnection(client, service);
}
catch (InterruptedIOException e) {}
catch (IOException e) {
if (ProxyServer._DEBUG_){
Logs.log("Listener 56:" + e);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -