Maximum buffer length for sendto?

被刻印的时光 ゝ 提交于 2019-11-27 01:59:41

问题


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


回答1:


Use getsockopt(). This site has a good breakdown of the usage and options you can retrieve.

In Windows, you can do:

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 discovery (it will check what the maximum UDP packet size is between here and the destination, and pick that), or if MTU discovery is off, it'll set the maximum size to the interface MTU and fragment anything larger. If you're sending over Ethernet, the typical MTU is 1500 bytes.




回答2:


On 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 *)&optval, &optlen);

Trying to send a bigger message (on Leopard 9216 octets on UDP sent via the local loopback) will result in "Message too long / EMSGSIZE".




回答3:


As UDP is not connection oriented there's no way to indicate that two packets belong together. As a result you're limited by the maximum size of a single IP packet (65535). The data you can send is somewhat less that that, because the IP packet size also includes the IP header (usually 20 bytes) and the UDP header (8 bytes).

Note that this IP packet can be fragmented to fit in smaller packets (eg. ~1500 bytes for ethernet).

I'm not aware of any OS restricting this further.



来源:https://stackoverflow.com/questions/25841/maximum-buffer-length-for-sendto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!