Reading all the data from a UDP socket
python, sockets, udp
Solution
You should always call recv with `65535` (the maximum size of a UDP packet) if you don't already know the size of the packet. Otherwise, when you call `recv`, the entire packet is marked as read and cleared from the buffer, but then only the first `128` bytes are fed to `msg_received`.
edit: when(if) you transition to receiving data only across a network, you can use a smaller number for `recv`, as noted in the Docs
Problem
Situation: I have a sendersocket which is bound to localhost UDP port 33100. I have a receiversocket socket bound to localhost UDP port 33101. The sender socket sends 4500 bytes of data (the string "hello man" * 500). On the receiver side, I have an epoll object which waits for the EPOLLIN event on the receiversocket. When there's an event, I do: ``` while True: msg_received = receiver_socket.recv(128) if msg_received.decode("UTF-8") == "": break else: print msg_received.decode("UTF-8") ``` Problem: The main problem is that I cannot read again after I have read the first 128 bytes of data. The sender side says it sent 4500 bytes of data as expected. If the sender sends the same 4500 bytes of data again, the EPOLLIN event is registered again and I read the new string. Somehow, the buffer get's cleared after my first read. Now even though the sender just sent me 4500 bytes of data, the first `recv` gives me 128 bytes of data and then nothing is `recv`ed after that. I am probably doing something really stupid so please enlighten me. I want to receive all the 4500 bytes of data.