Reputation: 2577
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
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
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 returningrecv
can read more than one complete message
If you're using TCP, you'll probably want to do something like this:
length
bytesYou could start with the readn
function.
Upvotes: 4