The determination of TCP and UDP packet size in socket communication

Source: Internet
Author: User

TCP, UDP packet Size determination

UDP and TCP protocols enable multiple applications to send and receive data simultaneously using the port number. The data is sent through the source port and is received through the destination port. Some network applications use only reserved or registered static ports, while others can use dynamic ports that are not registered. Because the UDP and TCP headers use two bytes to store the port number, the valid range for the port number is from 0 to 65535. Dynamic ports range from 1024 to 65535.

MTU Maximum transmission unit, this maximum transmission unit is actually closely related to the link layer protocol, and the structure of the ethernetii frame DMAC+SMAC+TYPE+DATA+CRC due to the limitations of the Ethernet transmission electrical Each Ethernet frame has a minimum size of 64bytes maximum can not exceed 1518bytes, for less than or greater than this limit of Ethernet frames we can see as the wrong data frame, the general Ethernet forwarding device will discard these data frames.

Since the largest data frame of Ethernet ethernetii is 1518Bytes, the frame head of the Ethernet frame is planed (Dmac destination MAC address 48bit=6bytes+smac source MAC address 48bit=6bytes+ Type field 2bytes 14Bytes and frame tail CRC checksum part 4Bytes then the place where the upper-layer protocol is left is the maximum value of 1500Bytes for the data domain. We call it the MTU.


The size of the UDP packet should be 1500-IP header (8) = 1472 (BYTES)
The TCP packet size should be the 1500-IP header-TCP header (s) = 1460 (BYTES)


Note *pppoe So-called PPPoE is running "PPP" on Ethernet. With the broadband access (this broadband access is generally cable modem or xDSL or Ethernet access), because the Ethernet lacks the authentication billing mechanism and the traditional operator is through the PPP protocol to the Dial-up service authentication billing, therefore introduces the PPPoE. PPPoE causes the MTU to become smaller the MTU of the Ethernet is 1500, minus the overhead (8Bytes) of the packet at the end of the PPP, which becomes 1492. However, most routing devices currently have an MTU of 1500.


If we define TCP and UDP packets that do not exceed the scope, then our package in the IP layer does not have to subcontract, so that the transmission process in the IP layer to avoid the packet error; If over the range, both IP datagram is greater than 1500 bytes, the sender IP layer will need to divide the packet into several slices, and the receiver IP layer needs to reorganize the datagram. More seriously, if the UDP protocol is used, the packet is discarded when the IP layer packet is wrong. A receiver cannot reorganize datagrams, causing the entire IP datagram to be discarded. UDP does not guarantee reliable transmission, but when TCP has a packet error, the packet is retransmitted to ensure reliable transmission.

The length of the UDP datagram is the total number of bytes including the header and the data portion, with the header length fixed and the data part variable. The maximum length of datagrams varies according to the operating environment. Theoretically, the maximum length of a datagram containing a header is 65535 bytes (64K).

When we are programming with the socket, the UDP protocol requires that the packet be less than 64k,tcp without qualification.

However, given the standard MTU value of 576 bytes on the Internet, it is recommended that UDP programming on the Internet be preferable to control the length of the data within a maximum of 548 bytes (576-8-20).

In terms of specific functions:

When sending with UDP protocol, the length of the maximum data can be sent by the SendTo function: 65535-ip header (8) = 65507 bytes. When sending data with the SendTo function, the function returns an error if the length of the sent data is greater than the value.

When sent with a TCP protocol, because TCP is a data flow protocol, there is no limit to packet size (the size of the buffer is not considered), which means that the data length parameter is unrestricted when the Send function is used. In fact, the specified data is not necessarily a one-time send out, if the data is longer, will be segmented sent, if relatively short, may wait and the next data sent together.

I have conceived in the process of programming such a subcontracting problem, so I found a few data on the Internet to sort out this log, I hope for everyone useful. Some textual citations have been indicated.

Article content part excerpt from: http://blog.sina.com.cn/s/blog_5c4dd3330100mnqr.html

Http://www.ylmf.net/zhuanti/zt02/2010/1110/9106.html

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.