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

Good website copywriting and good user experience

Looking at a website is actually like evaluating a...

Example code for element multiple tables to achieve synchronous scrolling

Element UI implements multiple tables scrolling a...

Vue advanced usage tutorial dynamic components

Table of contents Basic description AST parsing R...

How to install Solr 8.6.2 in Docker and configure the Chinese word segmenter

1. Environment version Docker version 19.03.12 ce...

How to call the interrupted system in Linux

Preface Slow system calls refer to system calls t...

Vue uses drag and drop to create a structure tree

This article example shares the specific code of ...

Docker container accesses the host's MySQL operation

background: There is a flask project that provide...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

Detailed explanation of how to detect and prevent JavaScript infinite loops

Table of contents Preface Fix infinite loop in fo...

Experience in designing a layered interface in web design

Many netizens often ask why their websites always ...

How to build php+nginx+swoole+mysql+redis environment with docker

Operating system: Alibaba Cloud ESC instance cent...

Summary of pitfalls of using nginx as a reverse proxy for grpc

background As we all know, nginx is a high-perfor...

MySQL learning database operation DML detailed explanation for beginners

Table of contents 1. Insert statement 1.1 Insert ...

Using zabbix to monitor the ogg process (Linux platform)

The ogg process of a database produced some time ...