Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About recv and the read buffer - C Berkeley Sockets

I am using berkeley sockets and TCP (SOCK_STREAM sockets).

The process is:

  1. I connect to a remote address.
  2. I send a message to it.
  3. I receive a message from it.

Imagine I am using the following buffer:

char recv_buffer[3000];
recv(socket, recv_buffer, 3000, 0);

Questions are:

  • How can I know if after calling recv first time the read buffer is empty or not? If it's not empty I would have to call recv again, but if I do that when it's empty I would have it blocking for much time.
  • How can I know how many bytes I have readed into recv_buffer? I can't use strlen because the message I receive can contain null bytes.

Thanks.

like image 288
NeDark Avatar asked Dec 06 '10 01:12

NeDark


2 Answers

How can I know if after calling recv first time the read buffer is empty or not? If it's not empty I would have to call recv again, but if I do that when it's empty I would have it blocking for much time.

You can use the select or poll system calls along with your socket descriptor to tell if there is data waiting to be read from the socket.

However, usually there should be an agreed-upon protocol that both sender and receiver follow, so that both parties know how much data is to be transferred. For example, perhaps the sender first sends a 2-byte integer indicating the number of bytes it will send. The receiver then first reads this 2-byte integer, so that it knows how many more bytes to read from the socket.

Regardless, as Tony pointed out below, a robust application should use a combination of length-information in the header, combined with polling the socket for additional data before each call to recv, (or using a non-blocking socket). This will prevent your application from blocking in the event that, for example, you know (from the header) that there should still be 100 bytes remaining to read, but the peer fails to send the data for whatever reason (perhaps the peer computer was unexpectedly shut off), thus causing your recv call to block.

How can I know how many bytes I have readed into recv_buffer? I can't use strlen because the message I receive can contain null bytes.

The recv system call will return the number of bytes read, or -1 if an error occurred.

From the man page for recv(2):

[recv] returns the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

like image 96
Charles Salvia Avatar answered Sep 21 '22 05:09

Charles Salvia


How can I know if after calling recv first time the read buffer is empty or not?

Even the first time (after accepting a client), the recv can block and fail if the client connection has been lost. You must either:

  • use select or poll (BSD sockets) or some OS-specific equivalent, which can tell you whether there is data available on specific socket descriptors (as well as exception conditions, and buffer space you can write more output to)
  • you can set the socket to be nonblocking, such that recv will only return whatever is immediately available (possibly nothing)
  • you can create a thread that you can afford to have block recv-ing data, knowing other threads will be doing the other work you're concerned to continue with

How can I know how many bytes I have readed into recv_buffer? I can't use strlen because the message I receive can contain null bytes.

recv() returns the number of bytes read, or -1 on error.

Note that TCP is a byte stream protocol, which means that you're only guaranteed to be able to read and write bytes from it in the correct order, but the message boundaries are not guaranteed to be preserved. So, even if the sender has made a large single write to their socket, it can be fragmented en route and arrive in several smaller blocks, or several smaller send()/write()s can be consolidated and retrieved by one recv()/read().

For that reason, make sure you loop calling recv until you either get all the data you need (i.e. a complete logical message you can process) or an error. You should be prepared/able to handle getting part/all of subsequent sends from your client (if you don't have a protocol where each side only sends after getting a complete message from the other, and are not using headers with message lengths). Note that doing recvs for the message header (with length) then the body can result in a lot more calls to recv(), with a potential adverse affect on performance.

These reliability issues are often ignored. They manifest less often when on a single host, a reliable and fast LAN, with less routers and switches involved, and fewer or non-concurrent messages. Then they may break under load and over more complex networks.

like image 29
Tony Delroy Avatar answered Sep 20 '22 05:09

Tony Delroy