Jump to content
  • Advertisement
Sign in to follow this  
pnroach

WinSock2 : how to figure out sent data size

This topic is 4451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi! i need to eceive data through call of rcv function, which also takes the size of received data as one of its parameters. but the thing is that i dunno the size of data i have to receive in advance. i saw people using ioctlsocket function with FIONREAD parameter to obtain the size of pending data but i always receive 0 after call to this function though there's data waiting to be read for sure. any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
It's very common to just have a big recieve buffer (64k say) and use that. If you app has a notion of a maximum message size you can use that instead. If you are using TCP you probably have a fixed-length header that includes the message size and you can read the header first, allocate your buffer, then read the rest of the message.

Share this post


Link to post
Share on other sites
Quote:
Original post by pnroachi need to eceive data through call of rcv function, which also takes the size of received data as one of its parameters. but the thing is that i dunno the size of data i have to receive in advance.


You misunderstand the purpose of that parameter. It is not the size of the recieved data, it's the number of bytes you want to receive. The common way to handle this is to use a large receive buffer and set the len parameter to the size of the buffer.

Two gotchas: you can wind up with more than one packet in the receive buffer and the last packet might be incomplete. It is important for you to have a good packet header that not only identifies each packet, but also tells you how large a packet is in bytes. This will allow you to loop through the buffer, split out multiple packets, and save incomplete packets for the next call to receive.

Share this post


Link to post
Share on other sites
@ Aldacron

hmm...

u mean i have to make my own custom header for every package i send?
i actually want to use sockets to send XML files between my apps.
maybe i could send just an integer value before each transfer which will hold the size of the next file that'll be sent...

is this a good alternative for a header?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!