SoFunction
Updated on 2025-03-10

UDP DUP timeout UPD port status detection code example

I've written an example before,Simple UDP server and client examples, it has been written that if you treat yourself as a client, then the client can specify its own port to send the number.

(5000); means the timeout time of the collection. If it is not set, it is waiting, which is longer than the love movies in TV series, and the result is the same. You will not wait any longer after you die. However, this timeout cannot be regarded as the timeout time of your request. Please pay attention to this concept, because this timeout is only used to mark that data has not been obtained from the network during this period, but even if the data is obtained, it may not be yours. You will understand this by looking at the example below.

Then there is the port issue. The above also said that you can specify the port yourself or treat yourself as a client. When you need to send data, create a connection object and then send data. This way the port is dynamic. This means that as long as the DatagramSocket object is not reinitialized or disappeared, the locally opened UDP port will not be closed.

Then there is the problem of UDP status. In fact, there was an article earlier.Understanding and use of UDP connection objects. Stateless means that this connection has no state. The ghost knows whether it has a server or not. The ghost doesn’t know whether it has a server or not even if that server is dead. But for local, if your DatagramSocket object always exists, then your local port is stateful and it is alive.

Then make an example:

package test;
import .*;
import .*;
import ;
/**
  * UDP client program, used to send data to the server and receive response information from the server
  */
public class UdpClientSocket {
	private byte[] buffer = new byte[1024];
	private static DatagramSocket ds = null;
	/**
	  * Test the method of sending and receiving response information by client
	  */
	public static void main(String[] args) throws Exception {
		UdpClientSocket client = new UdpClientSocket();
		String serverHost = "127.0.0.1";
		int serverPort = 10002;
		(serverHost, serverPort, new byte[]{1,2,3,4,5});
		while(true){
			byte[] bt = ();
			if(null != bt &&  > 0)
				("Data received:" + (bt));
			(1000);
		}
	}
	/**
	  * Constructor, create UDP client
	  */
	public UdpClientSocket() throws Exception {
		ds = new DatagramSocket(8899); // Bonding local port as client		(5000);
	}
	/**
	  * Send data information to the specified server
	  */
	public final void send(final String host, final int port,final byte[] bytes) throws IOException {
		DatagramPacket dp = new DatagramPacket(bytes, , (host), port);
		(dp);
	}
	/**
	  * Receive data sent back from the specified server
	  */
	public final byte[] receive() throws Exception {
		try {
			DatagramPacket dp = new DatagramPacket(buffer, );
			(dp);		
			byte[] data = new byte[()];
			((), 0, data, 0, ());		
			return data;
		} catch (Exception e) {
			();
			return null;
		}
	}
}

The operation keeps reporting an error:

: Receive timed out
at .receive0(Native Method)
at (:136)
at (:712)
at (:46)
at (:20)
: Receive timed out
at .receive0(Native Method)
at (:136)
at (:712)
at (:46)
at (:20)

Use TCPUDPDbg to send data to 8899 and you can receive:

Data received: [16, 17, 18, 19, 20]

This example has been stated

1. The local port is 8899

2. The timeout time of the collection is 5 seconds

3. Send a set of data to the local 10002 port, but do you know if you have received it

4. Continuously obtain UDP data received by local port 8899

Then I found

1. There is no error in sending data

2. Keep reporting errors and timeout

3. Use TCPUDPDbg to send data to 8899 to receive

Summarize:

Can specify the timeout of the collection number, but the timeout of each request needs to be controlled by yourself.

You can determine the number of local ports, and this port can survive statefully

There is no status, but there can be

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.