Reputation: 280000
I have a client application using winsock's sendto()
method to send data to a server application with UDP. On my client application I make, say, 5 quick sendto()
. On my server application, I wait for, say, 10 secs, and then do a select()
and recvfrom()
. Will the recvfrom()
give me the first packet sent by the client or will it be an arbitrary one (whichever one arrived first)? Will I still be able to get the other 4 data packets or does winsock's UDP framework only buffer one?
Upvotes: 1
Views: 3169
Reputation: 1499
UDP gives no guarantee to the received ordering of packets, so basically, the first packet you recvfrom()
might be the first packet you sent, but must not be - that's what TCP is for (which guarantees ordering of received data). You might not receive a part of the packets (or any, for that matter) at all if they were lost in transit.
For the second part: generally, the operating system will buffer a certain amount of packets for you, this depends on the socket buffer set up for UDP sockets - the buffer is specific to every socket, and not shared between them. On Windows, I'm not sure at how to get at the size of the buffer, on Linux, check out "/proc/sys/net/ipv4/udp_mem"; generally, you'll easily fit five UDP packets in there.
Upvotes: 1
Reputation: 41222
With 5 packets of reasonable size, you will probably get all of the packets and you will probably get the first one sent first. But they might be out of order, might not arrive, and might not contain the original data if they do arrive. You have to handle all of that yourself with UDP. (But depending on your application and requirements and the stability of your network, it might not be a real problem; it is certainly possible for certain situations to exist where receiving 99% of the data is perfectly fine).
Upvotes: 1
Reputation: 182649
Will the recvfrom() give me the first packet sent by the client or will it be an arbitrary one
Since UDP doesn't handle reordering, you can get any of the messages. You could get less than 4 messages or even more (but that's rare today).
Upvotes: 2