If my Server send 1000 bytes data. But my Client only only receive the first 4 byte. Is the rest of 996 bytes be held in somewhere?
Could I do that: My server send 1000 byte data. the first 4 bytes indicates the type of data Packet, the second 4 bytes indicates the size of the actual data which is 1000-4-4= 992 bytes.
So my Server only need to send the Packet. The Client will handle out what's the packet type, and how big the actual data is,and then receive the actual useful data ?
The only way for you to receive only 4 bytes of data when there's 1000 bytes available is to provide a buffer only 4 bytes long.
According to MSDN:
If the datagram or message is larger than the buffer specified, the buffer is filled with the first part of the datagram, and recv generates the error WSAEMSGSIZE. For unreliable protocols (for example, UDP) the excess data is lost; for reliable protocols, the data is retained by the service provider until it is successfully read by calling recv with a large enough buffer.
So that means:
If when you created the socket, for the second parameter (type) you provided SOCK_STREAM, that would be a reliable connection. So if your buffer is 4 bytes and the message is 1000 bytes, only the first 4 will be stored in the buffer, and the rest will remain there until you receive it with a buffer large enough. (Whether the data that remains in the buffer is 996 bytes or still 1000 bytes, I'm not sure; however, you can do an experiment yourself to know for sure.)
On the other hand, if you used SOCK_DGRAM, that would be an unreliable connection, so the first 4 bytes are read and the rest lost forever.
@Tyler T
First, thank your help. I just test out. with reliable protocols,the retained bytes in this example, If i receive the first 4 bytes, and then 996 bytes will be left. ; )