2

I have been working with Python 2.7's socket library for a few weeks now. A request has come up where I am asked to only receive part of a message being broadcasted over UDP. The message length is dynamic, but I am trying to provide a solution to only read the first part of this message. However, anytime I provide a buffer size smaller than the broadcasted buffer size, I receive the following error:

socket.error: [Errno 10040] A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself

This error confuses me, as I have read in the documentation and in multiple questions here on stack that this method will only read up to the provided buffer size and then throw away the rest. I am also pretty sure that this is not related to a network error because I can provide a buffer larger than the message size and it works correctly. Example code provided below:

import socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind((some_ip, some_udp_port))

# Scenario 1: Only want to read 244 bytes, but returns 10040 error. Have tried this with recvfrom as well. Have also tried 256 (for power of 2)
data = udp_socket.recv(244)

# Scenario 2: Provide buffer bigger than what is being sent, doesn't return 10040 error.
data = udp_socket.recv(1024)

Am I misunderstanding the purpose of the max buffer parameter? Is there anyway I can receive up to a certain amount of bytes per message?

cwio
  • 21
  • 1

0 Answers0