Meaning of FLAG in socket send and recv

user3751012 picture user3751012 · Jun 26, 2014 · Viewed 14.2k times · Source

While searching in the Linux manual page, what I have found about the format of send and recv in socket is like below:

For send,

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

For recv,

ssize_t recv(int sockfd, void *buf, size_t len, int flags);

But I am not sure what they are trying to tell about int flags. In one sample code I have found the value of flag as 0 (zero). What it means? Also what is the meaning of the line below in the man page?

"The flags argument is the bitwise OR of zero or more of the following flags."

Then the list of flags:

MSG_CONFIRM
MSG_DONTROUTE
.
.
.
etc.

Answer

macfij picture macfij · Jun 26, 2014

if int flags are equal to 0, it means that no flags are specified. these are optional.
to answer about ORing flags - it is a mechanism that allow you to specify more than one flag - MSG_CONFIRM | MSG_DONTWAIT specify two flags.

OR gate:          AND gate:

a b  out          a b  out
0 0  0            0 0  0
0 1  1            0 1  0
1 0  1            1 0  0
1 1  1            1 1  1

what i understand, is that by ORing flags you set specific bits to 1 in an int variable.
later in the code, by ANDing that variable with specific flags you know whether flag was set or not.

if you had specified MSG_DONTWAIT flag, the code: flags & MSG_DONTWAIT will return 1, so you know that flag was set.

let's have a look how MSG_DONTWAIT is defined.

enum
  {
    ...
    MSG_DONTWAIT    = 0x40, /* Nonblocking IO.  */
#define MSG_DONTWAIT    MSG_DONTWAIT
    ...
  };

the hex notation 0x40 means that only 7th bit is set to 1.

below i present an example of bitwise operations from socket.c. there's a check whether O_NONBLOCK flag was set when socket file descriptor was created. if so, then set on current flags variable 7th bit to 1, which was defined as MSG_DONTWAIT.

if (sock->file->f_flags & O_NONBLOCK)
    flags |= MSG_DONTWAIT;

a nice reference about bitwise operations: http://teaching.idallen.com/cst8214/08w/notes/bit_operations.txt