I've been reading through Beej's Guide to Network Programming 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 than 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 server side I'm also implementing his sendall()
function, so it should actually be sending everything to the client.
See also 6.1. A Simple Stream Server in the guide.
Yes, you have to loop over
recv()
until you receive'\0'
or an error happen (negative value fromrecv
) or0
fromrecv()
. 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 is 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
fromrecv
: this means that the connection was closed (it could be part of your protocol that this happens.)The first thing you need to learn when doing TCP/IP programming: 1
write
/send
call might take severalrecv
calls to receive, and several write/send calls might need just 1recv
call to receive. And anything in-between.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 untilrecv()
returns0
- 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.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)
, decreasingmissing
after eachrecv()
.