TCP and UDP packet transmission

Source: Internet
Author: User

UDP and TCP use port numbers to send and receive data simultaneously for multiple applications. Data is sent out through the source port and received through the target port. Some network applications can only use reserved or registered static ports, while others can use unregistered dynamic ports. Because UDP and TCP headers use two bytes to store port numbers, the valid range of port numbers is from 0 to 65535. The dynamic port range is from 1024 to 65535.

The maximum transmission unit of the MTU is closely related to the link layer protocol. Due to the electrical limitations of Ethernet transmission, the structure of the ethernetii frame DMAc + SMAC + Type + Data + CRC, each Ethernet frame has a minimum size of 64 bytes and a maximum size of 1518 bytes. For an Ethernet frame smaller than or greater than this limit, we can regard it as a wrong data frame, generally, Ethernet forwarding devices discard these data frames.

Since the maximum data frame of Ethernet ethernetii is 1518bytes, deplane the frame header of an Ethernet frame (DMAc destination MAC address 48bit = 6 bytes + SMAC source MAC address 48bit = 6 bytes + type domain 2 bytes) 14 bytes and the CRC check at the end of the frame is 4 bytes, so the rest of the places that carry the upper-layer protocol, that is, the maximum data domain can only have bytes, which we call MTU.


The UDP packet size should be 1500-IP header (20)-udp header (8) = 1472 (bytes)
The TCP packet size should be 1500-IP header (20)-TCP Header (20) = 1460 (bytes)


Note * the so-called pppoe is to run "PPP" over Ethernet ". With broadband access (this type of broadband access is generally cable modem or XDSL or Ethernet access ), due to the lack of authentication and billing mechanisms for Ethernet, the traditional carrier uses PPP protocol to authenticate and charge for dial-up and other access services, so pppoe is introduced. Pppoe causes the MTU to become smaller. The MTU of the Ethernet is 1500, and the overhead (8 bytes) of the end of the PPP packet header is reduced to 1492. However, the MTU of most routing devices is currently 1500.


If the defined TCP and UDP packets do not exceed the range, we do not need to subscribe the packets on the IP layer. In this way, the packet errors in the IP layer are avoided during transmission; if the range is exceeded, the sender's IP layer divides the data packet into several slices, and the receiver's IP layer needs to restructure the data packet. More seriously, if UDP protocol is used, when an error occurs in the IP layer group package, the package will be discarded. The receiver cannot reorganize the datagram, which will cause the entire IP datagram to be discarded. UDP does not guarantee reliable transmission. However, if a group packet error occurs in TCP, the packet will be re-transmitted to ensure reliable transmission.

The length of a UDP datagram refers to the total number of bytes including the header and data. The header length is fixed and the data part is variable. The maximum length of a datagram varies depending on the operating environment. Theoretically, the maximum length of a datagram containing a header is 65535 bytes (64 KB ).

When we use socket programming, the UDP protocol requires that the package be smaller than 64 KB, and there is no limit on TCP.

However, since the standard MTU value on the internet is 576 bytes, we recommend that you set the UDP data length to 548 bytes (576-8-20) during Internet UDP programming).

 

For specific functions:

The maximum length of data that can be sent using the sendto function is 65535-IP header (20)-udp header (8) = 65507 bytes. When you use the sendto function to send data, if the length of the sent data exceeds this value, the function returns an error.

When sending using the TCP protocol, TCP is a data stream protocol, so there is no limit on the package size (the buffer size is not considered for the time being). This means that when using the send function, the data length parameter is not limited. In fact, the specified data segment is not necessarily sent at one time. If the data segment is long, it will be sent in segments. If the data segment is short, it may wait for the data to be sent together with the next data segment.

UDP and TCP use port numbers to send and receive data simultaneously for multiple applications. Data is sent out through the source port and received through the target port. Some network applications can only use reserved or registered static ports, while others can use unregistered dynamic ports. Because UDP and TCP headers use two bytes to store port numbers, the valid range of port numbers is from 0 to 65535. The dynamic port range is from 1024 to 65535.

The maximum transmission unit of the MTU is closely related to the link layer protocol. Due to the electrical limitations of Ethernet transmission, the structure of the ethernetii frame DMAc + SMAC + Type + Data + CRC, each Ethernet frame has a minimum size of 64 bytes and a maximum size of 1518 bytes. For an Ethernet frame smaller than or greater than this limit, we can regard it as a wrong data frame, generally, Ethernet forwarding devices discard these data frames.

Since the maximum data frame of Ethernet ethernetii is 1518bytes, deplane the frame header of an Ethernet frame (DMAc destination MAC address 48bit = 6 bytes + SMAC source MAC address 48bit = 6 bytes + type domain 2 bytes) 14 bytes and the CRC check at the end of the frame is 4 bytes, so the rest of the places that carry the upper-layer protocol, that is, the maximum data domain can only have bytes, which we call MTU.


The UDP packet size should be 1500-IP header (20)-udp header (8) = 1472 (bytes)
The TCP packet size should be 1500-IP header (20)-TCP Header (20) = 1460 (bytes)


Note * the so-called pppoe is to run "PPP" over Ethernet ". With broadband access (this type of broadband access is generally cable modem or XDSL or Ethernet access ), due to the lack of authentication and billing mechanisms for Ethernet, the traditional carrier uses PPP protocol to authenticate and charge for dial-up and other access services, so pppoe is introduced. Pppoe causes the MTU to become smaller. The MTU of the Ethernet is 1500, and the overhead (8 bytes) of the end of the PPP packet header is reduced to 1492. However, the MTU of most routing devices is currently 1500.


If the defined TCP and UDP packets do not exceed the range, we do not need to subscribe the packets on the IP layer. In this way, the packet errors in the IP layer are avoided during transmission; if the range is exceeded, the sender's IP layer divides the data packet into several slices, and the receiver's IP layer needs to restructure the data packet. More seriously, if UDP protocol is used, when an error occurs in the IP layer group package, the package will be discarded. The receiver cannot reorganize the datagram, which will cause the entire IP datagram to be discarded. UDP does not guarantee reliable transmission. However, if a group packet error occurs in TCP, the packet will be re-transmitted to ensure reliable transmission.

The length of a UDP datagram refers to the total number of bytes including the header and data. The header length is fixed and the data part is variable. The maximum length of a datagram varies depending on the operating environment. Theoretically, the maximum length of a datagram containing a header is 65535 bytes (64 KB ).

When we use socket programming, the UDP protocol requires that the package be smaller than 64 KB, and there is no limit on TCP.

However, since the standard MTU value on the internet is 576 bytes, we recommend that you set the UDP data length to 548 bytes (576-8-20) during Internet UDP programming).

 

For specific functions:

The maximum length of data that can be sent using the sendto function is 65535-IP header (20)-udp header (8) = 65507 bytes. When you use the sendto function to send data, if the length of the sent data exceeds this value, the function returns an error.

When sending using the TCP protocol, TCP is a data stream protocol, so there is no limit on the package size (the buffer size is not considered for the time being). This means that when using the send function, the data length parameter is not limited. In fact, the specified data segment is not necessarily sent at one time. If the data segment is long, it will be sent in segments. If the data segment is short, it may wait for the data to be sent together with the next data segment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.