views:

1757

answers:

4

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  A: 

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.

Kristof Provost
+3  A: 
Steve M
A: 

@Steve M

Sound simple :) but which platforms do that work on? Not on Linux, IRIX, or MacOSX, can't find SO_MAX_MSG_SIZE on them...Windows only?

epatel
+3  A: 

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".

diciu