views:

1643

answers:

3

I've been reading through beejs guide to networking to get a handle on TCP connections. In one of the samples the Client code for a simple TCP stream client looks like:

 if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
     perror("recv");
     exit(1);
 }

 buf[numbytes] = '\0';

 printf("client: received '%s'\n",buf);

close(sockfd);

I've set the buffer to be smaller then the total number of bytes that I'm sending. I'm not quite sure how I can get the other bytes, Do I have to loop over recv() until I receive '\0'?

*Note on the serverside I'm also implementing his sendall() function so it should actually be sending everything to the client.

Beejs guide to networking

+6  A: 

Yes, you will need multiple recv() calls, until you have all data.

To know when that is, using the return status from recv() is no good - it only tells you how many bytes you have received, not how many bytes are available, as some may still be in transit.

It is better if the data you receive somehow encodes the length of the total data. Read as many data until you know what the length is, then read until you have received length data. To do that, various approaches are possible; the common one is to make a buffer large enough to hold all data once you know what the length is.

Another approach is to use fixed-size buffers, and always try to receive min(missing, bufsize), decreasing missing after each recv().

Martin v. Löwis
Yet another approach is to use end byte with special value (like ETX) if you are sure this value cannot appear within the message, or start byte (STX) and end byte (ETX). It is more difficult to handle on receiver side, but more robust. If something went wrong and some data lost in transmission, data flow can be easily synchronised with STX/ETX, but in case of length prefix all hell breaks loose
qrdl
+3  A: 

The first thing you need to learn when doing TCP/IP programming: 1 write/send call might take several recv calls to receive, and several write/send calls might need just 1 recv call to receive. And anything inbetween.

You'll need to loop until you have all data. The return value of recv() tells you how much data you received. If you simply want to receive all data on the TCP connection, you can loop until recv() returns 0 - provided that the other end closes the TCP connection when it is done sending.

If you're sending records/lines/packets/commands or something similar, you need to make your own protocol over TCP, which might be as simple as "commands are delimited with \n".

The simple way to read/parse such a command would be to read 1 byte at a time, building up a buffer with the received bytes and check for a \n byte every time. Reading 1 byte is extremely inefficient, so you should read larger chunks at a time.

Since TCP is stream oriented and does not provide record/message boundaries it becomes a bit more tricky - you'd have to recv a piece of bytes, check in the received buffer for a \n byte, if it's there - append the bytes to previously received bytes and output that message. Then check the remainder of the buffer after the \n - which might contain another whole message or just the start of another message.

nos
+1  A: 

Yes, you have to loop over recv() until you receive '\0' or an error happen (negative value from recv) or 0 from recv(). For the first option: only if this zero is part of your protocol (the server sends it). However from your code it seems that the zero it just to be able to use the buffer content as a C-string (on the client side).

The check for a return value of 0 from recv: this means that the connection was closed (it could be part of your protocol that this happens.)

Peter Mortensen
You can receive bytes with the value '\0'; the issue is when the count of the bytes returned is 0.
Jonathan Leffler