UDP DUP timeout UPD port status detection code example

UDP DUP timeout UPD port status detection code example

I have written an example before, a simple UDP server and client example, in which I wrote that if you regard yourself as a client, the client can specify its own port to send data.

ds.setSoTimeout(5000); is the timeout for data collection. If it is not set, it means waiting, which is longer than the waiting in the love movies in TV dramas, and the result is the same, that is, you will wait until you die and then stop waiting. However, this timeout period cannot be regarded as the timeout period of your request. Please pay attention to this concept, because this timeout period is only used to mark that no data has 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. As mentioned above, you can specify the port yourself, or you can treat yourself as a client. When you need to send data, you create a connection object and then send data. In this way, the port is dynamic. This means that as long as the DatagramSocket object is not reinitialized or disappears, the locally opened UDP port will not be closed.

Then there is the issue of UDP status. In fact, there is an article about this earlier, Understanding and Using UDP Connection Objects. Stateless means that this connection has no state. No one knows whether it has a server or not, and no one knows whether the server is dead or not. But for the local, if your DatagramSocket object always exists, then your local port is stateful and it is alive.

Then make an example:

package test;
import java.io.*;
import java.net.*;
import java.util.Arrays;
/**
 * UDP client program, used to send data to the server and receive the server's response information*/
public class UdpClientSocket {
	private byte[] buffer = new byte[1024];
	private static DatagramSocket ds = null;
	/**
	 * Test the client's method of sending packets and receiving response information */
	public static void main(String[] args) throws Exception {
		UdpClientSocket client = new UdpClientSocket();
		String serverHost = "127.0.0.1";
		int serverPort = 10002;
		client.send(serverHost, serverPort, new byte[]{1,2,3,4,5});
		while(true){
			byte[] bt = client.receive();
			if(null != bt && bt.length > 0)
				System.out.println("Received data: " + Arrays.toString(bt));
			Thread.sleep(1000);
		}
	}
	/**
	 * Constructor, create UDP client */
	public UdpClientSocket() throws Exception {
		ds = new DatagramSocket(8899); // Bind to local port as client ds.setSoTimeout(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, bytes.length, InetAddress.getByName(host), port);
		ds.send(dp);
	}
	/**
	 * Receive data sent back from the specified server */
	public final byte[] receive() throws Exception {
		try {
			DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
			ds.receive(dp);		
			byte[] data = new byte[dp.getLength()];
			System.arraycopy(dp.getData(), 0, data, 0, dp.getLength());		
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

Running always reports an error:

java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
at java.net.DatagramSocket.receive(DatagramSocket.java:712)
at test.UdpClientSocket.receive(UdpClientSocket.java:46)
at test.UdpClientSocket.main(UdpClientSocket.java:20)
java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
at java.net.DatagramSocket.receive(DatagramSocket.java:712)
at test.UdpClientSocket.receive(UdpClientSocket.java:46)
at test.UdpClientSocket.main(UdpClientSocket.java:20)

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

Received data: [16, 17, 18, 19, 20]

This example has been written

1. The local port is 8899

2. The timeout for receiving data is 5 seconds

3. A set of data was sent to the local port 10002. Who knows if it was received?

4. Continuously obtain UDP data received on local port 8899

Then found

1. No error is reported when sending data

2. The error message "Data collection timeout" is always displayed

3. Use TCPUDPDbg to send data to 8899 and it can be received

Summarize:

1.UDP can specify a timeout for receiving data, but the timeout for each request needs to be controlled by yourself

2.UDP can bind the local port number, and this port can survive in a state

3.UDP has no state, but local can have

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Usage and difference analysis of replace into and insert into on duplicate key update in MySQL
  • Java concurrency AtomicLongFieldUpdater atomic class_PowerNode Java Academy
  • The real difference between MySQL's Replace into and Insert into on duplicate key update
  • MySQL ON DUPLICATE KEY UPDATE Statement Examples
  • insert into … on duplicate key update / replace into Introduction to multiple rows of data
  • In-depth analysis of MySQL "ON DUPLICATE KEY UPDATE" syntax
  • Several operations of mysql insert (DELAYED, IGNORE, ON DUPLICATE KEY UPDATE)

<<:  The difference between delete, truncate, and drop and how to choose

>>:  Detailed description of shallow copy and deep copy in js

Recommend

Implementation of Vue package size optimization (from 1.72M to 94K)

1. Background I recently made a website, uidea, w...

Vue realizes the logistics timeline effect

This article example shares the specific code of ...

What are inline elements and block elements?

1. Inline elements only occupy the width of the co...

Implementation of CSS equal division of parent container (perfect thirds)

The width of the parent container is fixed. In or...

Drop-down menu and sliding menu design examples

I found a lot of websites that use drop-down or sl...

How to Easily Remove Source Installed Packages in Linux

Step 1: Install Stow In this example, we are usin...

Detailed tutorial on MySql installation and uninstallation

This article shares the tutorial of MySql install...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

Vue implements small form validation function

This article example shares the specific code of ...

A Brief Analysis of the Differences between “:=” and “=” in MySQL

= Only when setting and updating does it have the...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...