send(2) System Calls Manual send(2) send, sendto, sendmsg - LIBRARY Standard C library (libc, -lc) #include ssize_t send(int sockfd, const void buf[.len], size_t len, int flags); ssize_t sendto(int sockfd, const void buf[.len], size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); send(), sendto() sendmsg() . send() , ( ). send() write(2) flags. flags , send() write(2). , send(sockfd, buf, len, flags); sendto(sockfd, buf, len, flags, NULL, 0); sockfd . sendto() (SOCK_STREAM, SOCK_SEQPACKET), dest_addr addrlen ( EISCONN, NULL 0) ENOTCONN, . dest_addr addrlen. sendmsg() msg.msg_name, msg.msg_namelen. send() sendto() buf, len. sendmsg() msg.msg_iov. sendmsg() ( ). , EMSGSIZE . send() . -1. , send(), . , EAGAIN EWOULDBLOCK. , , select(2). flags : MSG_CONFIRM ( Linux 2.3.15) , : . , ( ARP). SOCK_DGRAM SOCK_RAW IPv4 IPv6. arp(7) . MSG_DONTROUTE , . . ; . MSG_DONTWAIT ( Linux 2.2) . , EAGAIN EWOULDBLOCK. O_NONBLOCK ( fcntl(2) F_SETFL), , MSG_DONTWAIT , O_NONBLOCK ( open(2)), , , , . MSG_EOR ( Linux 2.2) (record) ( , SOCK_SEQPACKET). MSG_MORE ( Linux 2.4.4) . TCP TCP_CORK (. tcp(7)), , . Linux 2.6 UDP , , ( UDP_CORK udp(7)). MSG_NOSIGNAL ( Linux 2.2) SIGPIPE, . EPIPE . sigaction(2) SIGPIPE, MSG_NOSIGNAL , SIGPIPE . MSG_OOB , (, , SOCK_STREAM); . MSG_FASTOPEN ( Linux 3.7) Attempts TCP Fast Open (RFC7413) and sends data in the SYN like a combination of connect(2) and write(2), by performing an implicit connect(2) operation. It blocks until the data is buffered and the handshake has completed. For a non-blocking socket, it returns the number of bytes buffered and sent in the SYN packet. If the cookie is not available locally, it returns EINPROGRESS, and sends a SYN with a Fast Open cookie request automatically. The caller needs to write the data again when the socket is connected. On errors, it sets the same errno as connect(2) if the handshake fails. This flag requires enabling TCP Fast Open client support on sysctl net.ipv4.tcp_fastopen. Refer to TCP_FASTOPEN_CONNECT socket option in tcp(7) for an alternative approach. sendmsg() msghdr, sendmsg(): struct msghdr { void *msg_name; /* */ socklen_t msg_namelen; /* */ struct iovec *msg_iov; /* / */ size_t msg_iovlen; /* # msg_iov */ void *msg_control; /* , */ size_t msg_controllen; /* */ int msg_flags; /* ( ) */ }; msg_name . ; msg_namelen . NULL 0, . msg_iov msg_iovlen /, writev(2). You may send control information (ancillary data) using the msg_control and msg_controllen members. The maximum control buffer length the kernel can process is limited per socket by the value in /proc/sys/net/core/optmem_max; see socket(7). For further information on the use of ancillary data in various socket domains, see unix(7) and ip(7). msg_flags . On success, these calls return the number of bytes sent. On error, -1 is returned, and errno is set to indicate the error. , . , ; . EACCES ( UNIX, ) (. path_resolution(7)). ( UDP) / , (unicast) . EAGAIN EWOULDBLOCK , . POSIX.1-2001 , , . EAGAIN ( ) , sockfd, , , . /proc/sys/net/ipv4/ip_local_port_range ip(7). EALREADY Fast Open. EBADF sockfd . ECONNRESET . EDESTADDRREQ . EFAULT . EINTR ; signal(7). EINVAL . EISCONN , ( , ). EMSGSIZE , (), . ENOBUFS . , , . , Linux . , . ENOMEM . ENOTCONN . ENOTSOCK sockfd . EOPNOTSUPP flags . EPIPE , , . SIGPIPE, MSG_NOSIGNAL. According to POSIX.1-2001, 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. MSG_CONFIRM is a Linux extension. 4.4BSD, SVr4, POSIX.1-2001. (first appeared in 4.2BSD). POSIX.1-2001 describes only the MSG_OOB and MSG_EOR flags. POSIX.1-2008 adds a specification of MSG_NOSIGNAL. sendmmsg(2) Linux, . Linux EPIPE ENOTCONN. sendto() getaddrinfo(3). . fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2), sendmmsg(2), shutdown(2), socket(2), write(2), cmsg(3), ip(7), ipv6(7), socket(7), tcp(7), udp(7), unix(7) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . send(2)