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:
int recv(int s, void *buf, int len, unsigned int flags);
The recv(2) function accepts four arguments, which are
- The socket s to receive data from (in-band or out-of-band data).
- The buffer buf to place the received data into.
- The maximum byte length (len) of the receiving buffer.
- 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:
char buf; /* 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.