How to receive udp messages continuously
The UDP header of a UDP packet consists of four fields, each of which occupies 2 bytes, as follows: source port number, target port number, datagram length check value UDP protocol reserves its own data transmission channel for different applications by using port numbers. UDP and TCP protocols use this mechanism to support multiple applications to send and receive data simultaneously. The data sender (client or server) sends UDP datagrams through the source port, while the data receiver receives data through the target port. Some network applications can only use static ports reserved or registered in advance; While other network applications can use unregistered dynamic ports. Because UDP headers use two bytes to store port numbers, the valid range of port numbers is from 0 to 65535. Generally speaking, port numbers greater than 49 15 1 represent dynamic ports. Tcp Packets Each tcp contains a source port number and a destination port number, plus the source ip and destination ip in the ip header to uniquely determine the TCP connection. The sequence number is used to identify the data byte stream sent from the tcp initiator to the tcp receiver, which represents the first data byte in the data segment. The serial number field contains the initial serial number isn (initial serial number) of the connection selected by this host. The host will send the first byte of data, and the sequence number is isn+ 1, because syn occupies a sequence number. The IPV4 header of an IP packet has 12 required fields and optional IP option fields, which are located before the data to be sent. If you use the existing libraries or other components of the IP layer, you generally don't need to consider most of the fields in the header, but the program code needs to provide the source address and destination address. The versions of 1 and version (4-bit) IP protocols have been modified many times. In 198 1, RFC079 1 describes IPV4 and RCF2460 introduces IPV6. 2. Header length (4 bits) The header length is the length of header data, which is expressed in 4 bytes, that is, in units of 32 bytes. The header length is variable. The required field uses 20 bytes (header length is 5, and IP option field has at most 40 additional bytes (header length is 15). 3. Service Type (8 digits) This field gives the method that the sending process suggests the router to handle the report. You can choose maximum reliability, minimum delay, maximum throughput and minimum overhead. Routers can ignore this part. 4. Datagram length (16 bits) This field is the sum of header length and data bytes, in bytes. The maximum length is 65535 bytes. 5. Identifier (16 bits) The original data host assigns a unique datagram identifier to the datagram. When the datagram is transmitted to the destination address, if the router divides the datagram into data segments, then each data segment has the same data identifier. 6. The flag (3 digits) 2 in the flag field is related to the movie report. No.0: Not used. Bit 1: Not a newspaper. If this bit is 1, the router will not split the datagram. Routers will try their best to transmit datagrams to networks that can receive complete datagrams at one time; Otherwise, the router will discard the datagram and return an error message indicating that the destination address is unreachable. The IP standard requires that the host can receive datagrams within 576 bytes. Therefore, if you want to send a datagram to an unknown host and want to ensure that the datagram will not be discarded because of its size, use data less than or equal to 576 bytes. Number two: More newspapers. If this bit is 1, the datagram is a fragment, but it is not the last fragment of a fragmented datagram; If this bit is 0, the datagram is not fragmented, or it is the last datagram. 7. Packet Offset (13 bits) This field identifies the position of the packet in the segmented datagram. Its value is 8 bytes, the maximum value is 8 19 1 byte, and the corresponding offset is 65528 bytes. For example, 1024 bytes to be sent are divided into 576 and 424 bytes. The first slice has an offset of 0 and the second slice has an offset of 72 (because 72× 8 = 576). 8. Time to Live (8 bits) If the datagram does not reach its destination within a reasonable time, the network will give it up. The time-to-live field determines when datagrams are dropped. Time to Live indicates the remaining time of the datagram, and each router will reduce its value by one, or reduce the time required to calculate and transmit the datagram. In fact, the time for routers to process and send datagrams is generally less than 1S, so this value does not measure time, but measures the number of hops or network segments between routers. The computer that sends the datagram sets the initial lifetime. 9. Protocol (8 bits) This field specifies the protocol used in the data part of the datagram, so the IP layer knows where to send the received datagram. The TCP protocol is 6 and the UDP protocol is 17. The word 10, header checksum (16 bits) makes the receiver of the datagram only need to check the errors in the IP header, without checking the contents of the data area or message. The checksum is calculated based on the value in the header, and the header checksum is assumed to be 0. Optional items in Ethernet frames, TCP segments and UDP datagrams all need message error detection. 1 1 and the source IP address (32 bits) indicate the sender of the datagram. 12, and the destination IP address (32 bits) indicates the destination of the datagram.