Reputation: 33
I need to make a communication on udp via python using buffer size 2048, I use the below code for listening from udp, however I lose the part after buffer size, and with the next message buffer size exceeded part of the packet does not come anymore. Shouldn't buffer size exceeded part stay on OS or kernel buffer, does it get lost when I receive the packet?
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', PORT))
s.setblocking(0)
print(s)
while (True):
print(s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF))
result = select.select([s], [], [])
msg = result[0][0].recv(2048)
msg = msg.decode('ascii')
print(len(msg))
print(isinstance(msg, str))
print(msg)
s.close()
Edit: Thx guys, now I understand that I should not send packages bigger than my buffer. Also it seems it is better to not use more than buffer size of 1500.
Upvotes: 1
Views: 844
Reputation: 39818
No, you cannot get the part of a UDP packet that was too big (regardless of IP-level fragmentation and reassembly). The point of datagrams is that they arrive as a unit; your logic will not be simplified by implementing additional layers of reassembly rather than just using a buffer that’s big enough (which you should know from your protocol).
Upvotes: 4