Sign in to follow this  
Tispe

Can recvfrom() read more then a packet?

Recommended Posts

Hi

I am wondering if recvfrom() given a lengthy buffer to read to would read more then a packet in non-blocking mode. Or will this read one packet at the time? Let's say I have a buffer[1400] passed to recvfrom() and the payload of the first packet in queue is 60 bytes and the next packet has 80 bytes. Will both packets be read in the same call?

It would simplyfy my code alot if only one packet was read at a time (packets may vary in size).

I am also concerned that sendto() may send less then the requested load and that if I should resend the rest of the information then that would show up as another packet on the reciever and be read as suggested above. If so I will have to have a spaciel header consisting of maybe 64 "1"'s (bit stuffing to prevent information to appear as a header), then a ID and a packet number in order to append it to the previous packet. (a third packet my come between the two).

I do not wish to go into serialization atm, I wish to just cast my structs into char, should work if I only work in Windows right?

Thanks

Share this post


Link to post
Share on other sites
When using UDP, only one packet is received per call to recv or recvfrom. Each call to send or sendo will create a single packet per call to send to the destination. But beware, do not try to send a packet that is too large, i.e. don't send a 5 MB packet. How do you figure out how big is too big? There is something called Maximum Transmission Unit, which is a term meaning the biggest size packet that will not be fragmented by the network that can be sent. Currently, you are guaranteed that a packet the size of 576 bytes will not be broken up when sending. But, most likely, a size of around 1440 bytes is safe. I am trying not to go into detail because there is alot of background information.
You should read up, http://www.garykessler.net/library/tcpip.html

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1307115731' post='4819093']
Have you read the [url="http://pubs.opengroup.org/onlinepubs/007908799/xns/recvfrom.html"]documentation[/url] for recvfrom()?
[/quote]

"Well, here's the second of the one-two punch: you might have read past the end of one packet and onto the next in a single [b]recv()[/b] call. "

I read this here: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html

So I got confused

Share this post


Link to post
Share on other sites
[quote name='Tispe' timestamp='1307116683' post='4819104']
[quote name='rip-off' timestamp='1307115731' post='4819093']
Have you read the [url="http://pubs.opengroup.org/onlinepubs/007908799/xns/recvfrom.html"]documentation[/url] for recvfrom()?
[/quote]

"Well, here's the second of the one-two punch: you might have read past the end of one packet and onto the next in a single [b]recv()[/b] call. "

I read this here: [url="http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html"]http://beej.us/guide...page/bgnet.html[/url]

So I got confused
[/quote]


recv() and recvfrom() are different functions with different properties.

recv() is stream-based and can extract more or less than a single game-transmitted packet.

recvfrom() has various options that can enable it to read exactly one packet, optionally discard data, or operate as a stream-based read.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this