Friday, May 21, 2010

[c++ <sys/socket.h> recv function] how can I find out how long is message caught by recv?

I'm writing a program using sockets.


How can I find out how long is message send to my by other computer when i caught it by recv() function.

[c++ %26lt;sys/socket.h%26gt; recv function] how can I find out how long is message caught by recv?
recv() returns an int containing (1) the number of bytes read or (2) -1 if there was an error or (3) zero bytes when the other end has shutdown properly.





Strictly speaking there is no magic way of knowing what the peer is sending. The message protocol must be worked out in some meaningful way in advance. A message may end with a carriage return. Or a certain number of bytes. Or maybe there is a header with a counter telling your side how much to expect. However it is performed it must be coordinated and understood in advance by both sides.





Note that you may not receive the whole message in one recv() call. You may need to make repeated calls and store tthe results from each in a buffer somewhere until everything you expect (or need to continue processing) has been received.
Reply:I'm writing a prorgam using sockets.


No comments:

Post a Comment