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?