Summary of questions about socket packet size (UDP transfer mode)

Source: Internet
Author: User

Recently contacted Linux programming, in the PC computer two socket communication program to do the test, one in the way of TCP, the other using UDP. Increasing the size of the transmission packet to 180k size, UDP traffic is not received packets, blocking in the Recvfrom (), while the TCP mode is still functional.

For a socket created with SOCK_DGRAM to send a UDP packet, if the data sent is too large, the TCP/IP stack under Linux kernel is sent back into the packet, and the application does not need to care about the size of the data.

The problem is that the size of the data sent exceeds the maximum limit of the kernel buffer, which seems to be 65535 (64K by default)
By using function setsockopt (IP_SOCKET->SOCKET.FD, Sol_socket, So_rcvbuf, &size, &len), change the size of the receive buffer. Similarly, setsockopt (IP_SOCKET->SOCKET.FD, Sol_socket, So_sndbuf, &size, &len) can also change the size of the send buffer.

#define RMEM_MAX (1024*200)
{/
   * DMA: The kernel defaults to receive BUF 64K, working at 0. 14MB/S, when the speed is greater than 19MB/S, Linux
will lose the packet   can change   /proc/sys/net/core/rmem_max this file.
You can change the echo 200000 >/proc/sys/net/core/rmem_max using the following command.
   *
   /FILE *f;
   Char buf[80];
   int   Rmax;
   f = fopen ("/proc/sys/net/core/rmem_max", "RW");
   if (f) {
    fread (buf, 1, sizeof (BUF), f);
    SSCANF (buf, "%d", &rmax);     Reads an int assignment from the Rmax to buf
    fclose (f);
    if (Rmax = = 65535) {
     sprintf (buf, "%d", Rmem_max);    The New_rmem_max is exported in%d to buf
     f = fopen ("/proc/sys/net/core/rmem_max", "w");
     Fwrite (buf, 1, strlen (BUF) +1, f);
     Fclose (f);
     New_rmem_max = Rmem_max;
    } else
     New_rmem_max = Rmax;
   }
}

Combined with the above information summary:

1.UDP transmission of a piece of complete data is limited by the kernel buffer, for example, by default, the buffer size is 64K, so for the socket receiver or sender in that case, the maximum can be sent one 64K of data at a time. For the bottom of the socket, the concrete implementation is to split the 64K data into n small packets, the internal delivery mechanism guarantees the reliability of the data, such as 1 k per packet, divided into 64 packages, sent from the server side to the client, and the client in the local buffer to complete the submission of the full data to the user. The underlying implementations should be different, but the results are the same for the upper layers. If the buffer size is exceeded, there is no way to complete the packet loss and so on. Similarly, for 200K, you can send data that receives a 200K size.

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.