如何在不阻塞 ServerSocket 的情况下获取待处理请求的数量或接受请求?
How to get number of pending requests or accept requests without blocking in ServerSocket?
我创建了 java.net.ServerSocket
的子类。
在 run
方法中,我想获取所有待处理的请求并向它们发送错误消息。
如何使用 ServerSocket
?
这是我的代码:
public class SafeWalkServer extends ServerSocket implements Runnable {
public SafeWalkServer(int port) throws IOException {
super(port);
}
@Override
public void run() {
try {
boolean isShutdown = false;
while (!isShutdown) {
Socket client = accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter writer = new PrintWriter(client.getOutputStream());
if (clientText.startsWith(":RESET"))
**//at this point I need to send error message to all pending requests.**
Socket sock = accept();
}
}
}
}
我需要待处理请求的数量或 accept
的非阻塞变体?
你可以用 ServerSocketChannel. call configureBlocking(false) to do non-blocking. There are many good tutorials (with sample code) like The Rox Java NIO Tutorial.
做非阻塞套接字
我创建了 java.net.ServerSocket
的子类。
在 run
方法中,我想获取所有待处理的请求并向它们发送错误消息。
如何使用 ServerSocket
?
这是我的代码:
public class SafeWalkServer extends ServerSocket implements Runnable {
public SafeWalkServer(int port) throws IOException {
super(port);
}
@Override
public void run() {
try {
boolean isShutdown = false;
while (!isShutdown) {
Socket client = accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter writer = new PrintWriter(client.getOutputStream());
if (clientText.startsWith(":RESET"))
**//at this point I need to send error message to all pending requests.**
Socket sock = accept();
}
}
}
}
我需要待处理请求的数量或 accept
的非阻塞变体?
你可以用 ServerSocketChannel. call configureBlocking(false) to do non-blocking. There are many good tutorials (with sample code) like The Rox Java NIO Tutorial.
做非阻塞套接字