我在用Java的套接字苦苦挣扎。我需要设置一个超时,以便我的进程放弃并在1000毫秒后停止运行。我尝试遵循documentation,以及此处关于stackoverflow的一些帖子,但是在调用accept()
函数时,该过程一直处于等待,阻塞状态。我在做什么错了?
private static void statusRequest(String destAddr) throws ClassNotFoundException {
try {
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER2);
serverSocket.setSoTimeout(1000);
Socket socket = serverSocket.accept(); // Blocking function
// [... Expected working flow ...]
// [... do some work with the received object ...]
} catch (SocketException s) {
System.out.println("No message received");
}
}