When your talent is not enough to satisfy your ambition, you should calm down and study hard

Overview of UDP

  • UDP is the User Datagram Protocol (UDP), a connectionless transport layer Protocol in the OSI (Open System Interconnection) reference model. Providing a simple transaction-oriented unreliable message transfer service, IETF RFC 768 is the official specification of UDP. The protocol number of UDP in the IP packet is 17.
  • UDP protocol is the full name of the user datagram protocol, in the network it is the same as TCP protocol for processing data packets, is a connectionless protocol. In the OSI model, the fourth layer, the transport layer, is one layer above the IP protocol. UDP has the disadvantage of not providing packet grouping, assembly, and ordering of packets. That is to say, after the packet is sent, there is no way to know whether it has arrived safely and completely. UDP is used to support network applications that need to transfer data between computers. UDP protocol is needed in many network applications of client/server mode, including network video conferencing system. UDP protocol has been used for many years since its inception. Although its initial luster has been overshadowed by some similar protocols, UDP is still a very practical and feasible network transport layer protocol even today.

Key features of UDP

  • UDP is a connectionless protocol in which no connection is made between the source and the terminal before data is transmitted. When it wants to transmit, it simply grabs the data from the application and throws it over the network as quickly as possible. On the sending side, the speed at which UDP can transmit data is limited only by the speed at which the application can generate data, the power of the computer, and the transmission bandwidth. On the receiving side, UDP puts each message segment on a queue, from which the application reads one at a time.
  • Since the transmission of data does not establish a connection, there is no need to maintain the connection state, including the sending and receiving state, etc., so a server machine can simultaneously transmit the same message to multiple clients.
  • The UDP packet header is short, only 8 bytes, and UDP has very little overhead compared to TCP’s 20-byte packet.
  • Throughput is not regulated by congestion control algorithms and is limited only by the rate of data generated by the application software, the transmission bandwidth, and the performance of the source and terminal hosts.
  • UDP is message oriented. The message handed down by the sender’s UDP to the application, after adding a header, is delivered down to the IP layer. Instead of splitting or merging, the boundaries of these packets are preserved, so the application needs to choose the appropriate packet size.

Although UDP is an unreliable protocol, it is an ideal protocol for distributing information. For example, report the stock market on the screen, display aviation information, and so on. UDP is also used in the RIP Routing Information Protocol to modify Routing tables. In these applications, if a message is lost, a new message will replace it a few seconds later. UDP is widely used in multimedia applications.

The difference between TCP and UDP

  • TCP is a connection-oriented transmission control protocol, while UDP provides connectionless datagram service.
  • TCP has high reliability, to ensure the correctness of data transmission, not missing or out-of-order, while UDP don’t establish a connection before transmitting data, incorrect data check and modification, do not need to wait for the reply of the other party, so there will be a packet loss, repetition, out-of-order, applications need to be responsible for all transmission reliability;
  • UDP has better real-time performance and higher working efficiency than TCP protocol.
  • UDP segment structure is simpler than TCP segment structure, so the network overhead is also small;
  • TCP protocol can ensure that the receiver can receive the byte stream sent by the sender without any mistake, and provide reliable communication service for the application program. Communication systems with high reliability requirements often use TCP to transmit data.

The main application

Applicable occasions

You must be careful when choosing UDP as the transport protocol. In the environment where the network quality is very unsatisfactory, the packet loss of UDP protocol will be more serious. However, due to the characteristics of UDP: it does not belong to the connection protocol, so it has the advantages of low resource consumption and fast processing speed, so usually audio, video and ordinary data are used more in the transmission of UDP, because even if they lose one or two packets occasionally, it will not have too much impact on the received results. For example, we chat with ICQ and QQ is the use of UDP protocol.

The practical application

In the field of measurement and control, it is oriented to distributed controllers, monitors, etc., whose application environment is relatively bad, so it puts forward different requirements for the transmission of data, such as real-time, anti-interference, security, etc. Based on this, in the field communication, if an application wants to transmit a set of data to another node in the network, the data can be transmitted to the IP process by the UDP process with the header. The UDP protocol eliminates the process of establishing and disconnecting the connection, cancels the retransmission inspection mechanism, and can achieve a higher communication rate.

Code demo

Simple example of client/server data sending and receiving

UDP Client:

Throws IOException {public static void main(String[] args) throws IOException {public static void main(String[] args) throws IOException = new DatagramSocket(); // create a data packet String MSG = "hello server ~"; InetAddress localhost = InetAddress.getByName("localhost"); int port = 9090; DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port); // send the packet socket.send(packet); // close the stream socket.close(); }}

UDP server:

Public class UDPServer {public static void main(String[] args) throws IOException {datagramSocket Socket = new DatagramSocket(9090); // Byte [] Bytes = new Byte [1024]; DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length); // block receiving socket.receive(packet); String msg = new String(packet.getData(),0,packet.getLength()); System.out.println(" data received from the client: "+ MSG); // Close the data stream socket.close(); }}

Simulate a conversation between a student and a teacher

// public class TalkSend implements Runnable {DatagramSocket socket = null; BufferedReader br = null; private int formPort; private String toIp; private int toPort; public TalkSend(int formPort, String toIp, int toPort) { this.formPort = formPort; this.toIp = toIp; this.toPort = toPort; try { socket = new DatagramSocket(this.formPort); BR = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { String msg = br.readLine(); byte[] bytes = msg.getBytes(); DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIp, this.toPort)); // send data socket.send(packet); if (msg.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); }}
// public class TalkReceive implements Runnable {DatagramSocket socket = null; private int port; private String msgForm; public TalkReceive(int port, String msgForm) { this.port = port; this.msgForm = msgForm; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); }} @Override public void run() {while (true) {try {Byte [] Bytes = new Byte [1024]; DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length); socket.receive(packet); Bye String MSG = new String(packet. GetData (), 0, packet. GetLength ()); System.out.println(msgForm + ":" + msg); if ("bye".equals(msg)) { break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); }}
public class TalkStudent { public static void main(String[] args) { new Thread(new TalkSend(7777,"localhost",8888)).start(); New Thread(new TalkReceive(9999," Teacher ")).start(); }}
public class TalkTeacher { public static void main(String[] args) { new Thread(new TalkSend(5555,"localhost",9999)).start(); New Thread(new TalkReceive(8888," Student ")).start(); }}

Test it and the results are as follows:

conclusion

  • UDP user datagram protocol, is a connectionless communication protocol, UDP data includes destination port number and source port number information, because communication does not need to connect, so it can be broadcast.
  • UDP communication does not need to be confirmed by the receiver, which is an unreliable transmission and may cause packet loss. Programmers are required to program and verify in practical applications.