gpt4 book ai didi

java - 未抛出 SocketTimeoutException

转载 作者:行者123 更新时间:2023-12-01 21:38:31 26 4
gpt4 key购买 nike

以下代码应根据是否达到超时抛出异常:

public boolean isAlive(int workerNum) throws Exception
{
System.out.println("Checking worker #" + workerNum + " from " +
getWorkerAddress(workerNum)
+ " at port " + getWorkerPort(workerNum));

DatagramPacket packet = new DatagramPacket("__ping__".getBytes(), "__ping__".length(),
getWorkerAddress(workerNum), getWorkerPort(workerNum));
socket.setSoTimeout(10000);
try {
System.out.println("Checking worker #" + workerNum);
socket.send(packet);
} catch (SocketTimeoutException e) {
e.printStackTrace();
return false;
}
return true;
}

我已经尝试了所有可能的情况,并且我可以保证数据包永远不会被抛出,因为它永远不会在另一端收到。知道为什么吗?非常感谢任何帮助!

最佳答案

The following code should throw an exception based on whether a timeout was reached:

不,不应该。 DatagramSocket.setSoTimeout() 设置读取超时。对写作没有影响。

关于java - 未抛出 SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com