Search code examples
cflagsrecv

C - How to add multiple flags


Is this the right way to add multiple flags into a function?

recv(sfd, &buf, sizeof(buf), MSG_DONTWAIT | MSG_ERRQUEUE);

I don't get an error message in my buf.
recv() does not block.
I get errno: 11, which says "try again".


Solution

  • The way you have added flags to the last parameter to recv() is fine. It seems you do not understand what the MSG_DONTWAIT will do.

    The MSG_DONTWAIT flag will cause the recv() call to be performed as a non-blocking operation. That means it will return -1 with errno set to EAGAIN or EWOULDBLOCK if there is no data to be returned.

    MSG_DONTWAIT (since Linux 2.2)

    • Enables nonblocking operation; if the operation would block, the call fails with the error EAGAIN or EWOULDBLOCK. This provides similar behavior to setting the O_NONBLOCK flag (via the fcntl(2) F_SETFL operation), but differs in that MSG_DONTWAIT is a per-call option ...

    man 2 recv

    If you want recv() to block until there is data returned, remove the MSG_DONTWAIT flag, and make sure your socket is does not set O_NONBLOCK.