mmtauqir
mmtauqir

Reputation: 9329

Reading all the data from a UDP socket

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 recved after that.

I am probably doing something really stupid so please enlighten me. I want to receive all the 4500 bytes of data.

Upvotes: 4

Views: 10989

Answers (2)

Will
Will

Reputation: 4528

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

Upvotes: 8

eqzx
eqzx

Reputation: 5609

If you know that you'll be getting 4500 bytes of data, you can call receiver_socket.recv(4500)

What your code is saying is that the maximum bytes to read is 128 with receiver_socket.recv(128)

See the python documentation for sockets

Upvotes: 1

Related Questions