Sunday, September 11, 2011

Reading Out-of-Band Data


Out-of-band data can be read in two different ways:
  • Read separately as out-of-band data 
  • Read intermixed with the in-band data
In order to read out-of-band data separately from the normal stream of data, you need to use the function recv(2). If you guessed that recv(2) is like read(2) with an additional flags argument, then you guessed correctly. The function prototype is shown as follows:

#include <sys/types.h>
#include <sys/socket.h>

int recv(int s, void *buf, int len, unsigned int flags);

The recv(2) function accepts four arguments, which are
  1. The socket s to receive data from (in-band or out-of-band data). 
  2. The buffer buf to place the received data into. 
  3. The maximum byte length (len) of the receiving buffer. 
  4. The option flags to use for this call.
As you can see, recv(2) is a counterpart to the send(2) function call. To receive out-of-band data, supply the C macro MSG_OOB in the flags argument. Without flag bit MSG_OOB, normal inband
data is received by the recv(2) function as if the normal read (2) call were made instead.

The recv(2) function returns the number of bytes received or -1 if an error occurred (check errno for the cause of the error). The following shows an example of reading out-of-band data:

Example

char buf[128]; /* Buffer */
int n;         /* No. of bytes */
int s;         /* Socket */
int len;       /* Max bytes */
. . .
n = recv(s, buf, len, MSG_OOB);

Although it was indicated earlier that out-of-band data could optionally be intermixed with normal data, we will defer this discussion until later.

No comments:

Post a Comment