What is the transmission efficiency of UDP and what are the factors involved?
It is intuitively believed that the larger the packet length of UDP, the higher the efficiency should be sent (up to 65536). However, according to the actual test and the data found on the Internet, the package length of 1024 is the most efficient delivery.
This kind of result is confusing, why is 1024 such strange value? Why not the length of the MTU (the smallest sending unit) (that is, 1500-28)?
Later, the survey found that the network bottom of Windows, the default UDP shard length of 1024, the way is fast channel mode, how fast channel? No further research is going on.
You can increase by 1024 by modifying the registry below.
Hkey_local_machine\system\currentcontrolset\services\afd\parameters\fastsenddatagramthreshold
And the MTU of the network adapter registry needs to be modified to match the above values, as shown in the following registry entries:
Hkey_local_machine\\system\currentcontrolset\services\tcpip\parameters\interfaces\mtu
Sample code to modify the above registry value:
Modify the local UDP packet send length and determine the packet length that the actual UDP sends based on the network MTU
<span style= "font-family:arial, Helvetica, Sans-serif;" >lenpacket "Out": Package length, </span><span style= "font-family:arial, Helvetica, Sans-serif;" >bistorestartcomputer "Out": whether to restart the computer (when the registry is modified, restart is valid) </span>
<pre name= "code" class= "CPP" ><span style= "font-family:arial, Helvetica, Sans-serif;" >//return: TRUE (Success), FALSE (failed) </span>
BOOL Setmaxefficencyudppacketlength (INT &lenpacket, BOOL &bistorestartcomputer) {//Initialize Lenpacket = MAX_SUPER_ display_udp_length;//setting "Hkey_local_machine\system\currentcontrolset\services\afd\parameters"// Add fastsenddatagramthreshold=1500if (! Cutil::iskeyexist (HKEY_LOCAL_MACHINE, _t ("System\\currentcontrolset\\services\\afd\\parameters"), _T (" FastSendDatagramThreshold ")) {//If the write fails, follow the send if (! Cutil::writekey (HKEY_LOCAL_MACHINE, _t ("System\\currentcontrolset\\services\\afd\\parameters"), _T (" FastSendDatagramThreshold "), Mtu_default) {lenpacket = Not_set_fast_send_datagrame_udp_length;return FALSE;} Bistorestartcomputer = TRUE;} Checksum value Else{int Ivalue = 0;//Gets the key value if (Cutil::readkey (HKEY_LOCAL_MACHINE, _t ("system\\currentcontrolset\\services\\afd\ \parameters "), _t (" FastSendDatagramThreshold "), Ivalue) {//If the key value is not the MTU default, modify if (ivalue! = Mtu_default) {//If the write fails, Then follow the Send if (! Cutil::writekey (HKEY_LOCAL_MACHINE, _t ("System\\currentcontrolset\\services\\afd\\parameters"), _T (" FastSendDatagramThreshold ") (Mtu_default)) {lenpacket = Not_set_fast_send_datagrame_udp_length;return FALSE;} Bistorestartcomputer = TRUE;}}} Set "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"//Modify mtu=1500//if not present, Then return hkey hMainKey = NULL; LONG Lretcode =:: RegOpenKeyEx (hkey_local_machine,_t ("system\\currentcontrolset\\services\\tcpip\\parameters\\ Interfaces "), 0,key_read,&hmainkey); if (lretcode! = ERROR_SUCCESS) {return TRUE;} Traverse all network ports, modify Mtudword dwindex = 0; TCHAR Swzsubkey[max_path] = _t ("");D word Dwnamelen = max_path;while (Error_success = =:: RegEnumKeyEx (hMainKey, DwIndex, Swzsubkey, &dwnamelen, NULL, NULL, NULL, NULL)) {//constructor child key full path CString Strfullsubkey = _t ("system\\currentcontrolset\\ Services\\tcpip\\parameters\\interfaces\\ ") Strfullsubkey + = swzsubkey;//Modify Mtuif (Cutil::iskeyexist (HKEY_LOCAL_ Machine, Strfullsubkey, _t ("MTU"))) {INT Ivalue = 0;//Gets the key value if (Cutil::readkey, HKEY_LOCAL_MACHINE, Strfullsubkey ("MTU "), Ivalue) {//If the key value is not the MTU default value, modify if (ivalue! = Mtu_default) {//If the write fails, follow the send if (! Cutil::writekey (HKEY_LOCAL_MACHINE, Strfullsubkey, _t ("MTU"), Mtu_default)) {Lenpacket = Not_set_fast_send_datagrame _udp_length;return FALSE;} Bistorestartcomputer = TRUE;}}} Reset Cache memset (swzsubkey, 0, sizeof (swzsubkey));d Wnamelen = max_path;//Next subkey dwindex ++;}::regclosekey (hMainKey); return TRUE;}
However, it is important to note that modifying this value requires ensuring that the MTU is less than or equal to the entire network path, and how does the MTU of the entire network be checked? can be obtained by executing the following command:
Ping-f-N 1-l 1472 192.168.0.2
Where 1472 is the packet length sent, if the execution result is 0, it is able to send, can continue to increase by 1472, or lower, until the maximum value is obtained.
The specific code looks like this:
Get network mtu//Uldestip "in": Target ip//return: Mtuuint getlanmtu (ULONG uldestip) {//Initialize UINT Lenpacket = Not_set_fast_send_ datagrame_udp_length;//instruction Cache Char szcmdbuf[middle_buf_length];memset (szcmdbuf, 0, sizeof (SZCMDBUF));//Construction Instructions sprintf_s (Szcmdbuf, Middle_buf_length, "Ping-f-N 1-l 1472%s", Inet_ntoa (* (struct in_addr *) &uldestip));//execute instruction int iRet = SYS TEM (SZCMDBUF);//If the MTU is if (IRet = = 0) {lenpacket = Max_super_display_udp_length;} Else{cutil::outputconsolelogstring ("LAN ' s MTU isn ' t 1500");} return lenpacket;}
about how to improve UDP delivery efficiency