当数据包未从服务器发送时,如何在 UDP 客户端中设置超时?
How to set a timeout in a UDP client for when a packet is not sent from server?
我正在编写一个关于 UDP 套接字编程的简单程序。我正在使用数据报套接字。我必须将数据包从客户端发送到服务器。然后服务器随机决定是否发回数据包。客户端必须接受发送的数据包或等待 2 秒并假设数据包丢失。我无法处理数据包丢失的情况。
System.out.println("Receiving message...");
dsock.receive(dpack); // receive the packet
System.out.println("Message received");
如果发送了数据包,一切正常,但是当没有发送数据包并且我仍然有这行代码时,我该如何处理?
您正在搜索 dsock.setSoTimeout(2 * 1000)
(2*1000 = 2000 毫秒 = 2 秒)。这是 doc
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
这将在两秒后引发 SocketTimeoutException,因此您必须捕获它。
您可以更改套接字的超时并接收消息,直到达到超时,如下所示:
try {
dsock = new DatagramSocket();
byte[] buf = new byte[1000];
DatagramPacket dpack = new DatagramPacket(buf, buf.length);
//...
dsock.setSoTimeout(1000); // set the timeout in millisecounds.
while(true) { // recieve data until timeout
try {
System.out.println("Receiving message...");
dsock.receive(dpack); // receive the packet
System.out.println("Message received");
}
catch (SocketTimeoutException e) {
// timeout exception.
System.out.println("Timeout reached!!! " + e);
dsock.close();
}
}
catch (SocketException e) {
System.out.println("Socket closed " + e);
}
我正在编写一个关于 UDP 套接字编程的简单程序。我正在使用数据报套接字。我必须将数据包从客户端发送到服务器。然后服务器随机决定是否发回数据包。客户端必须接受发送的数据包或等待 2 秒并假设数据包丢失。我无法处理数据包丢失的情况。
System.out.println("Receiving message...");
dsock.receive(dpack); // receive the packet
System.out.println("Message received");
如果发送了数据包,一切正常,但是当没有发送数据包并且我仍然有这行代码时,我该如何处理?
您正在搜索 dsock.setSoTimeout(2 * 1000)
(2*1000 = 2000 毫秒 = 2 秒)。这是 doc
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
这将在两秒后引发 SocketTimeoutException,因此您必须捕获它。
您可以更改套接字的超时并接收消息,直到达到超时,如下所示:
try {
dsock = new DatagramSocket();
byte[] buf = new byte[1000];
DatagramPacket dpack = new DatagramPacket(buf, buf.length);
//...
dsock.setSoTimeout(1000); // set the timeout in millisecounds.
while(true) { // recieve data until timeout
try {
System.out.println("Receiving message...");
dsock.receive(dpack); // receive the packet
System.out.println("Message received");
}
catch (SocketTimeoutException e) {
// timeout exception.
System.out.println("Timeout reached!!! " + e);
dsock.close();
}
}
catch (SocketException e) {
System.out.println("Socket closed " + e);
}