Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP Message too long

I'm trying to send data over UDP and wondering why the maximum data length is limited to 9253 bytes on my system (Mac OS X 10.9).

This is how I send data (simplified):

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 9999
MESSAGE = "A"*9217

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

and I got the error

Traceback (most recent call last):
  File "Untitled 2.py", line 8, in <module>
    sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
socket.error: [Errno 40] Message too long

In fact, the maximum "string length" I can transfer is 9216. When I do a byte size check on the client side via

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 9999

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((UDP_IP, UDP_PORT))
data, addr = self.sock.recvfrom(65535)
print("Received {0} bytes of data.".format(sys.getsizeof(data)))

I get

Received 9253 bytes of data.

I don't understand why it is 9253 bytes long, when I send 9216 bytes (this is the string length in bytes). 28 bytes is the UDP header but what is stored in the remaining 9 bytes?

And my main problem: how do I send and receive UDP packets up to 65535 bytes?

like image 606
tamasgal Avatar asked Apr 02 '14 17:04

tamasgal


1 Answers

I had the same problem and found a solution for your [Errno 40]. The problem lies not in the MTU. The MTU is the maximum size for 1 package that can be sent. For Ethernet this MTU is 1500.

However, computers can fragment a UDP-package that is larger than the MTU into packages that are smaller. That way you should be able to send udp-packages up to 65535.

Now we come to your problem. By default OSX has limited the maximum UDP-package to be 9216 bytes, don't ask me why. You can alter this value using the following command in the terminal.

    sudo sysctl -w net.inet.udp.maxdgram=65535

This change will not survive a reboot.

Beware that if one of the fragments does not reach its client, the whole package is dropped. That is why it is smarter to keep the size of your datagrams under the MTU.

like image 130
jusx Avatar answered Sep 24 '22 09:09

jusx