📄 listenthread.java
字号:
// $Id: ListenThread.java,v 1.3 2001/01/17 01:47:12 nconway Exp $
package tornado;
import java.net.*;
import java.io.IOException;
public class ListenThread extends Thread {
private final ServerPool serverPool;
private final int port;
private ServerSocket serverSocket;
/** Constructs a new thread with the specified values.*/
public ListenThread(ServerPool serverPool, int port) {
this.serverPool = serverPool;
this.port = port;
}
/** Begins an infinite loop, accepting and dispatching incoming
* connections.
*/
public void run() {
bindToPort();
while (true) {
listen();
}
}
/** Connects Tornado to a local system port. If an error is encountered,
* Tornado aborts.
*/
private void bindToPort() {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// treat all I/O errors as fatal
throw new RuntimeException(e.getMessage());
}
}
/** Waits for, accepts and dispatches a single incoming connection.*/
private void listen() {
try {
Socket connection = serverSocket.accept();
serverPool.dispatch(connection);
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -