recv(2) System Calls Manual recv(2) recv, recvfrom, recvmsg - Standard C library (libc, -lc) #include ssize_t recv(int sockfd, void buf[.size], size_t size, int flags); ssize_t recvfrom(int sockfd, void buf[restrict .size], size_t size, int flags, struct sockaddr *_Nullable restrict src_addr, socklen_t *_Nullable restrict addrlen); ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); recv(), recvfrom() recvmsg() . , , . , . recv() read(2) flags. flags , recv() read(2) ( ). , recv(sockfd, buf, size, flags); recvfrom(sockfd, buf, size, flags, NULL, NULL); All three calls return the size of the message on successful completion. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from. If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and errno is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested. select(2), poll(2) epoll(7). flags : MSG_CMSG_CLOEXEC ( recvmsg(); Linux 2.6.23) close-on-exec , UNIX, SCM_RIGHTS ( unix(7)). O_CLOEXEC open(2). MSG_DONTWAIT ( 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, whereas O_NONBLOCK is a setting on the open file description (see open(2)), which will affect all threads in the calling process as well as other processes that hold file descriptors referring to the same open file description. MSG_ERRQUEUE ( Linux 2.2) . ( IPv4 IP_RECVERR). . cmsg(3) ip(7). , , msg_iovec. , , msg_name. sock_extended_err: #define SO_EE_ORIGIN_NONE 0 #define SO_EE_ORIGIN_LOCAL 1 #define SO_EE_ORIGIN_ICMP 2 #define SO_EE_ORIGIN_ICMP6 3 struct sock_extended_err { uint32_t ee_errno; /* Error number */ uint8_t ee_origin; /* Where the error originated */ uint8_t ee_type; /* Type */ uint8_t ee_code; /* Code */ uint8_t ee_pad; /* Padding */ uint32_t ee_info; /* Additional information */ uint32_t ee_data; /* Other data */ /* More data may follow */ }; struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *); ee_errno errno . ee_origin . . SO_EE_OFFENDER , . , sa_family sockaddr AF_UNSPEC, sockaddr . , , . ( , cmsg_len cmsghdr). MSG_ERRQUEUE msghdr. , , . MSG_OOB , . , . MSG_PEEK , . , . MSG_TRUNC ( Linux 2.2) For raw (AF_PACKET), Internet datagram (since Linux 2.4.27/2.6.8), netlink (since Linux 2.6.22), and UNIX datagram as well as sequenced-packet (since Linux 3.4) sockets: return the real size of the packet or datagram, even when it was longer than the passed buffer. tcp(7). MSG_WAITALL ( Linux 2.2) . , , , , , , , . . recvfrom() recvfrom() places the received message into the buffer buf. The caller must specify the size of the buffer in size. src_addr NULL, , , src_addr. addrlen -. , src_addr. addrlen . , ; addrlen , . , src_addr addrlen NULL. recv() recv(), , ( connect(2)). : recvfrom(fd, buf, size, flags, NULL, 0); recvmsg() recvmsg() msghdr. : struct msghdr { void *msg_name; /* Optional address */ socklen_t msg_namelen; /* Size of address */ struct iovec *msg_iov; /* Scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void *msg_control; /* Ancillary data, see below */ size_t msg_controllen; /* Ancillary data buffer size */ int msg_flags; /* Flags on received message */ }; The msg_name field points to a caller-allocated buffer that is used to return the source address if the socket is unconnected. The caller should set msg_namelen to the size of this buffer before this call; upon return from a successful call, msg_namelen will contain the size of the returned address. If the application does not need to know the source address, msg_name can be specified as NULL. msg_iov msg_iovlen /, readv(2). The field msg_control, which has size msg_controllen, points to a buffer for other protocol control-related messages or miscellaneous ancillary data. When recvmsg() is called, msg_controllen should contain the size of the available buffer in msg_control; upon return from a successful call it will contain the size of the control message sequence. : struct cmsghdr { size_t cmsg_len; /* ( -- socklen_t POSIX) */ int cmsg_level; /* */ int cmsg_type; /* , */ /* unsigned char cmsg_data[]; */ }; , cmsg(3). As an example, Linux uses this ancillary data mechanism to pass extended errors, IP options, or file descriptors over UNIX domain sockets. For further information on the use of ancillary data in various socket domains, see unix(7) and ip(7). recvmsg() msg_flags msghdr. : MSG_EOR : ( SOCK_SEQPACKET). MSG_TRUNC , , , . MSG_CTRUNC , - . MSG_OOB , . MSG_ERRQUEUE , , . MSG_CMSG_CLOEXEC (since Linux 2.6.23) indicates that MSG_CMSG_CLOEXEC was specified in the flags argument of recvmsg(). -1, . errno . (shutdown), 0 ( << >>). Datagram sockets in various domains (e.g., the UNIX and Internet domains) permit zero-size datagrams. When such a datagram is received, the return value is 0. 0 , 0. , . , ; . EAGAIN EWOULDBLOCK , , . POSIX.1 , , . EBADF sockfd . ECONNREFUSED ( , ). EFAULT . EINTR , ; signal(7). EINVAL . ENOMEM recvmsg(). ENOTCONN , , , (. connect(2) accept(2)). ENOTSOCK sockfd . According to POSIX.1, the msg_controllen field of the msghdr structure should be typed as socklen_t, and the msg_iovlen field should be typed as int, but glibc currently types both as size_t. POSIX.1-2008. POSIX.1-2001, 4.4BSD ( 4.2BSD). POSIX.1 MSG_OOB, MSG_PEEK MSG_WAITALL. If a zero-size datagram is pending, read(2) and recv() with a flags argument of zero provide different behavior. In this circumstance, read(2) has no effect (the datagram remains pending), while recv() consumes the pending datagram. recvmmsg(2) Linux, . recvfrom() getaddrinfo(3). fcntl(2), getsockopt(2), read(2), recvmmsg(2), select(2), shutdown(2), socket(2), cmsg(3), sockatmark(3), ip(7), ipv6(7), socket(7), tcp(7), udp(7), unix(7) () aereiae , Azamat Hackimov , Dmitriy S. Seregin , Katrin Kutepova , Lockal , Yuri Kozlov , , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux man-pages 6.12 17 2024 . recv(2)