Maximum buffer length for sendto?

How do you get the maximum number of bytes that can be passed to the sendto(..) call for a socket opened as a UDP port?

+7
unix udp networking sockets ioctl
source share
3 answers

Use getsockopt (). This site has a good breakdown of usage and parameters that you can get.

On Windows, you can:

 int optlen = sizeof (int);
 int optval;
 getsockopt (socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (int *) & optval, & optlen);

For Linux, according to the UDP man page, the kernel will use MTU detection (it will check what maximum UDP packet size is between this destination and destination, and select this), or if MTU detection is turned off, set the maximum MTU interface size and larger fragment. If you are sending over Ethernet, a typical MTU is 1,500 bytes.

+11
source share

In Mac OS X, there are different values ​​for sending (SO_SNDBUF) and receiving (SO_RCVBUF). This is the size of the send buffer (man getsockopt):

getsockopt (sock, SOL_SOCKET, SO_SNDBUF, (int *) and optval, and optlen);

Attempting to send a larger message (on Leopard 9216 octets to UDP sent through a local loopback) will result in the message being too long / EMSGSIZE.

+5
source share

Since UDP is not connection oriented, there is no way to indicate that two packets belong to each other. As a result, you are limited by the maximum size of a single IP packet (65535). The data you can send is slightly smaller because the size of the IP packet also includes an IP header (usually 20 bytes) and a UDP header (8 bytes).

Please note that this IP packet may be fragmented to fit in smaller packets (e.g. ~ 1500 bytes for ethernet).

I do not know which OS limits this.

+1
source share

All Articles