C - 如何添加多个标志
C - How to add multiple flags
这是将多个标志添加到函数中的正确方法吗?
recv(sfd, &buf, sizeof(buf), MSG_DONTWAIT | MSG_ERRQUEUE);
我的 buf
中没有收到错误消息。
recv()
不阻塞。
我得到 errno: 11,上面写着 "try again".
您将标志添加到 recv()
的最后一个参数的方式很好。看来你不明白 MSG_DONTWAIT
会做什么。
MSG_DONTWAIT
标志将导致 recv()
调用作为非阻塞操作执行。这意味着它将 return -1
与 errno
设置为 EAGAIN
或 EWOULDBLOCK
如果没有数据被 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 ...
如果您希望 recv()
阻塞直到有数据 returned,请删除 MSG_DONTWAIT
标志,并确保您的套接字未设置 O_NONBLOCK
。
这是将多个标志添加到函数中的正确方法吗?
recv(sfd, &buf, sizeof(buf), MSG_DONTWAIT | MSG_ERRQUEUE);
我的 buf
中没有收到错误消息。
recv()
不阻塞。
我得到 errno: 11,上面写着 "try again".
您将标志添加到 recv()
的最后一个参数的方式很好。看来你不明白 MSG_DONTWAIT
会做什么。
MSG_DONTWAIT
标志将导致 recv()
调用作为非阻塞操作执行。这意味着它将 return -1
与 errno
设置为 EAGAIN
或 EWOULDBLOCK
如果没有数据被 returned.
MSG_DONTWAIT
(since Linux 2.2)
- Enables nonblocking operation; if the operation would block, the call fails with the error
EAGAIN
orEWOULDBLOCK
. This provides similar behavior to setting theO_NONBLOCK
flag (via thefcntl
(2)F_SETFL
operation), but differs in thatMSG_DONTWAIT
is a per-call option ...
如果您希望 recv()
阻塞直到有数据 returned,请删除 MSG_DONTWAIT
标志,并确保您的套接字未设置 O_NONBLOCK
。