deniz
deniz

Reputation: 2577

Receiving part of data with recv(), is that possible?

I'm using non-blocking sockets with winsock and I wonder that if I can partially receive data ?

My packet contains a "length" WORD and I must first read it then read whole packet according to the "length".

Actually this question is like "how does recv() work and end ?", Can i use recv() until I got all the data ?

Upvotes: 1

Views: 2059

Answers (2)

dextrey
dextrey

Reputation: 855

For TCP socket: You can use recv in a loop until you have got enough bytes. Note that recv may return less bytes than you requested. In that case just keep calling until you have the whole message.

Upvotes: 2

cnicutar
cnicutar

Reputation: 182734

It depends on the type of the socket. If it's a datagram socket, recv will read exactly one entire datagram. If it's a TCP socket:

  • recv will read at least one byte before returning
  • recv can read more than one complete message

If you're using TCP, you'll probably want to do something like this:

  • Read at least the bytes comprising the length
  • Read length bytes
  • You now have a complete message
  • Rinse, repeat

You could start with the readn function.

Upvotes: 4

Related Questions