Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling partial return from recv() TCP in C

Tags:

c++

c

tcp

sockets

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.

like image 474
whatWhat Avatar asked Sep 06 '09 17:09

whatWhat


People also ask

What does recv () return?

If successful, recv() returns the length of the message or datagram in bytes. The value 0 indicates the connection is closed.

How does RECV work in C?

The recv() function shall return the length of the message written to the buffer pointed to by the buffer argument. For message-based sockets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be read in a single operation.

Is RECV a blocking call in C?

The recv() call blocks until data arrives on the socket. While it is blocked, other threads that are waiting for the lock are also blocked. Although this example is specific to network I/O, the recv() call could be replaced with any blocking call, and the same behavior would occur.

What happens when you call read () or recv ()) on an open socket UDP or TCP and there is no data datagrams or bytes in the buffer to be read?

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and errno is set to EAGAIN or EWOULDBLOCK.


2 Answers

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

like image 103
Martin v. Löwis Avatar answered Oct 11 '22 02:10

Martin v. Löwis


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.

like image 44
nos Avatar answered Oct 11 '22 02:10

nos