UDP - User Datagram Protocol

A, UDP protocol: User DatagramProtocol

  A non-transport layer protocol connection, providing simple transaction-oriented unreliable messaging service

Features:

  • Non-connection-oriented, reliable transmission is not, data may be lost;
  • Pre and post connection will not be disconnected during data transmission;
  • The sender regardless of whether the other party is ready, the recipient does not receive confirmation;
  • You can broadcast transmission;
  • Very simple protocol overhead small;

  Each output operation process are exactly generates the next UDP datagram, and assembled into an IP datagram to be transmitted, as shown in the format:
Here Insert Picture Description
  application must be concerned about the length of the IP datagram. If it exceeds the MTU of the network, then it would have to be fragmented IP datagram. If desired, each network between the source to the destination should be fragmented, not just sending host connected to a first network to do so.

Two, UDP header

Here Insert Picture Description
  UDP checksum covers the UDP header and UDP, UDP and TCP headers are covered test header portion and data and, the UDP checksum is optional, and TCP checksum is required;
  the length of the UDP datagram may be an odd number bytes, but the checksum algorithm is the number of 16 bit words are added. The solution is added at the end, if necessary padding byte 0, this is merely for checksum calculations (i.e., may increase the stuffing bytes is not transmitted), secondly, UDP and TCP datagram contains a 12 byte segment long pseudo-header, which is provided in order to calculate the checksum. Pseudo-header includes some of the IP header field. Its purpose is to check whether the data twice UDP has reached the correct destination.
  If the sender is not calculated and the checksum test and the receiver detects an error, then the UDP datagram will be silently
discarded. Does not produce any error packet (IP layer when the IP header checksum is detected and when there is an error to do the same), UDP checksum is a checksum end. It is calculated by the sender, and then verified by the receiving terminal. Its purpose is to find the UDP header and data sender to any changes between the receiving end. Despite UDP checksums are optional, but they should always be in use.
  Host Requirements RFC statement, UDP checksum option under the default condition is open. It is also stated that if the transmitting side have been calculated and tested, then the receiver shall examine the received checksum (e.g., the received checksum is not zero). But many systems do not comply with this, just verify the received inspection and export inspection and only when the option is turned on.

Three, UDP programming exercises
  • Server
public class UDPServer {
    public static void main(String[] args) throws IOException {
        System.out.println("接收方启动中...");
        //1、使用DatagramSocket 指定端口,创建接收端
        DatagramSocket socket = new DatagramSocket(9999);
        //2、准备容器 封装成DatagramPacket包裹
        byte[] bytes = new byte[1024]; //
        DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
        //3、阻塞式接收包裹 receive(DatagramPacket p)
        socket.receive(packet);
        //4、分析数据
        byte[] datas = packet.getData();
        int length = packet.getLength();
        System.out.println(new String (datas,0,length));
        //5、释放资源
        socket.close();
    }
}
  • Client
public class UDPClient {
    public static void main(String[] args) throws IOException {
        System.out.println("发送方启动中...");
        //1、使用DatagramSocket 指定端口,创建发送端
        DatagramSocket socket = new DatagramSocket(8888);
        // 2、准备数据 转成字节数组
        String s = "daria";
        byte[] bytes = s.getBytes();
        // 3、封装成DatagramPacket包裹,需要指定目的地
        DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,
                new InetSocketAddress("localhost" ,9999));
        // 4、发送包裹 send(DatagramPacket p)
        socket.send(packet);
        // 5、释放资源
        socket.close();
    }
}
Fourth, the distinction Socket programming based on TCP and UDP protocols
Based on Socket Programming TCP protocol Socket programming based on UDP protocol
The two sides need to establish a communication connection Communicating parties without establishing a connection
The two sides primary or secondary connection is established Communication both full equality

Streaming services : data is a data source, there is no limit of data transmission times and reception times is not directly related,
     the receiver in the data buffer, a data can not be received completely, the buffer can be placed, secondary read;
datagram service : transmission times and reception times are equal,
      if a receiving end fails to complete the read data transport layer, the remaining data is discarded directly.

Guess you like

Origin blog.csdn.net/Daria_/article/details/90720084