One, the introduction

User Datagram Protocol (UDP) is a simple packet – oriented transport layer Protocol. Each output operation of a process produces exactly one UDP protocol (with a length limit). This is different for stream-oriented character protocols. UDP is encapsulated in IP packets. UDP does not provide reliability: it sends data from an application to the IP layer, but does not guarantee that it will reach its destination.

2. UDP structure

  • Source port number: 2 bytes in length, identifying the port on the source host from which the packet is sent.
  • Destination port number: a string of 2 bytes, identifying the port number of the destination host.
  • UDP length: 2 bytes, indicating the length of the UDP header and UDP data, in bytes. The minimum value of this field is 8 bytes (it is OK to send a 0 byte UDP packet). This UDP length is redundant. The IP packet length refers to the full length of the packet, so the UDP packet length is the full length minus the length of the IP header.
  • UDP verifies and overwrites UDP headers and UDP data. Recall that the IP header checksum covers only the IP header. Both UDP datagrams and TCP segments contain a 12-byte pseudo-header, which is set up for calculating checksums. The pseudo header contains some fields of the IP header. The goal is to have UDP double check that the data has arrived at its destination correctly.

* Because the IP layer has assigned IP packets to TCP or UDP (according to the protocol field value in the IP header), TCP views the TCP port number and UDP views the UDP port number. The TCP port and UDP port are independent of each other. *

3. IP fragmentation

The physical network layer generally limits the maximum length of each data frame to be sent. When the length of data to be sent exceeds the MTU, the data is fragmented and transmitted. An IP packet is fragmented and reassembled only when it reaches its destination (unlike other network protocols, which require it to be reassembled at the next station, not the final destination). Reassembly is done by the destination IP layer, and the purpose is to make the sharding and reassembly process transparent to the transport layer (TCP and UDP), except for some possible off-level operations.

  • 16-bit Identity field: The Identity field contains a unique value for each IP packet sent by the sender. This value is copied to each slice when the datagram is sharded.
  • 3-bit flag field (FLAG) : The flag field uses the last bit to indicate “more slices”. This bit is set to 1 for every slice that makes up the datagram except the last. The middle bit is the “non-sharding” bit. If this bit is set to 1, IP will not fragment the datagram.
  • 13-bit slice offset: This packet is offset from the beginning of the original packet. In addition, when the packet is fragmented, the total length value of each piece is changed to the length value of the packet.

To capture the packet, execute the following PHP code:


      
$handle = stream_socket_client("The udp: / / 110.12.15.10:9999", $errno, $errstr);
if( !$handle ){    
      die("ERROR: {$errno} - {$errstr}\n");
}
$str = 'start';
for($i=0; $i<500; $i++){ $str .='abcdefjhijklmnopkrstuvwxyz1234567890';
}
$str .= 'end';
fwrite($handle, $str."\n");
Copy the code

See here, do you want to scan the QR code to follow the wechat public account Linwan Village Dragon cat.