Handling partial return from recv() TCP in C

26,926

Solution 1

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().

Solution 2

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 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 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.

Share:
26,926
whatWhat
Author by

whatWhat

Updated on August 05, 2020

Comments

  • whatWhat
    whatWhat almost 4 years

    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.

  • Jonathan Leffler
    Jonathan Leffler over 14 years
    You can receive bytes with the value '\0'; the issue is when the count of the bytes returned is 0.
  • user207421
    user207421 over 8 years
    Why would he receive a \0? Unless he sent one? There is no evidence in the question about that.