send(2)
๐ send(2) โ send, sendto, sendmsg ๐
okay bestie, this is send(2) from the System Calls Manual, and it's the trio of syscalls you use to slide into another socket's DMs. sit down, hydrate, let's go โจ
๐ธ NAME ๐ธ
send, sendto, sendmsg โ send a message on a socket ๐
๐ LIBRARY ๐
Standard C library (libc, -lc)
โจ SYNOPSIS โจ
#include <sys/socket.h>
ssize_t send(size_t size;
int sockfd, const void buf[size], size_t size, int flags);
ssize_t sendto(size_t size;
int sockfd, const void buf[size], size_t size, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
๐ DESCRIPTION ๐
okay so. The system calls send(), sendto(), and sendmsg() are used to transmit a message to another socket. that's the whole gig. main character behavior fr fr ๐
The send() call may be used only when the socket is in a connected state (so that the intended recipient is known โ like, you already have them in your contacts). The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2). Also, this call:
send(sockfd, buf, size, flags);
is equivalent to
sendto(sockfd, buf, size, flags, NULL, 0);
The argument sockfd is the file descriptor of the sending socket. periodt.
If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the arguments dest_addr and addrlen are ignored (and the error EISCONN may be returned when they are not NULL and 0), and the error ENOTCONN is returned when the socket was not actually connected ๐ญ. Otherwise, the address of the target is given by dest_addr with addrlen specifying its size. For sendmsg(), the address of the target is given by msg.msg_name, with msg.msg_namelen specifying its size.
For send() and sendto(), the message is found in buf and has size size. For sendmsg(), the message is pointed to by the elements of the array msg.msg_iov. The sendmsg() call also allows sending ancillary data (also known as control information) ๐ โ yes, extra little gifts tucked into your packet.
If the message is too long to pass atomically through the underlying protocol, the error EMSGSIZE is returned, and the message is not transmitted. no partial sends, no crumbs, all or nothing ๐
No indication of failure to deliver is implicit in a send(). Locally detected errors are indicated by a return value of -1.
When the message does not fit into the send buffer of the socket, send() normally blocks, unless the socket has been placed in nonblocking I/O mode. In nonblocking mode it would fail with the error EAGAIN or EWOULDBLOCK in this case. The select(2) call may be used to determine when it is possible to send more data. iykyk ๐ฆ
๐ฉ The flags argument ๐ฉ
The flags argument is the bitwise OR of zero or more of the following flags. mix and match, go wild ๐
MSG_CONFIRM(since Linux 2.3.15) โ Tell the link layer that forward progress happened: you got a successful reply from the other side. If the link layer doesn't get this it will regularly reprobe the neighbor (e.g., via a unicast ARP). Valid only onSOCK_DGRAMandSOCK_RAWsockets and currently implemented only for IPv4 and IPv6. Seearp(7)for details.MSG_DONTROUTEโ Don't use a gateway to send out the packet, send to hosts only on directly connected networks. This is usually used only by diagnostic or routing programs. This is defined only for protocol families that route; packet sockets don't. it's giving "no middlemen" ๐ซMSG_DONTWAIT(since Linux 2.2) โ Enables nonblocking operation; if the operation would block,EAGAINorEWOULDBLOCKis returned. This provides similar behavior to setting theO_NONBLOCKflag (via thefcntl(2)F_SETFLoperation), but differs in thatMSG_DONTWAITis a per-call option, whereasO_NONBLOCKis a setting on the open file description (seeopen(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. so like, one-time vibe vs. permanent personality trait ๐MSG_EOR(since Linux 2.2) โ Terminates a record (when this notion is supported, as for sockets of typeSOCK_SEQPACKET). that's a wrap on this record bestie ๐ฌMSG_MORE(since Linux 2.4.4) โ The caller has more data to send. This flag is used with TCP sockets to obtain the same effect as theTCP_CORKsocket option (seetcp(7)), with the difference that this flag can be set on a per-call basis.
Since Linux 2.6, this flag is also supported for UDP sockets, and informs the kernel to package all of the data sent in calls with this flag set into a single datagram which is transmitted only when a call is performed that does not specify this flag. (See also the UDP_CORK socket option described in udp(7).) "wait wait I'm not done typing" energy ๐ญ
MSG_NOSIGNAL(since Linux 2.2) โ Don't generate aSIGPIPEsignal if the peer on a stream-oriented socket has closed the connection. TheEPIPEerror is still returned. This provides similar behavior to usingsigaction(2)to ignoreSIGPIPE, but, whereasMSG_NOSIGNALis a per-call feature, ignoringSIGPIPEsets a process attribute that affects all threads in the process.MSG_OOBโ Sends out-of-band data on sockets that support this notion (e.g., of typeSOCK_STREAM); the underlying protocol must also support out-of-band data.MSG_FASTOPEN(since Linux 3.7) โ Attempts TCP Fast Open (RFC7413) and sends data in the SYN like a combination ofconnect(2)andwrite(2), by performing an implicitconnect(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 returnsEINPROGRESS, 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 sameerrnoasconnect(2)if the handshake fails. This flag requires enabling TCP Fast Open client support on sysctlnet.ipv4.tcp_fastopen.
Refer to TCP_FASTOPEN_CONNECT socket option in tcp(7) for an alternative approach.
๐ง sendmsg() ๐ง
The definition of the msghdr structure employed by sendmsg() is as follows:
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 (unused) */
};
The msg_name field is used on an unconnected socket to specify the target address for a datagram. It points to a buffer containing the address; the msg_namelen field should be set to the size of the address. For a connected socket, these fields should be specified as NULL and 0, respectively. respect the rules bestie ๐ซถ
The msg_iov and msg_iovlen fields specify scatter-gather locations, as for writev(2).
You may send control information (ancillary data) using the msg_control and msg_controllen members. The maximum control buffer size 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). โจ
The msg_flags field is ignored. it's just there. decorative. a prop ๐
๐ RETURN VALUE ๐
On success, these calls return the number of bytes sent. On error, -1 is returned, and errno is set to indicate the error.
๐ ERRORS ๐
These are some standard errors generated by the socket layer. Additional errors may be generated and returned from the underlying protocol modules; see their respective manual pages. (so like, your faves might have more drama than listed here)
EACCESโ (For UNIX domain sockets, which are identified by pathname) Write permission is denied on the destination socket file, or search permission is denied for one of the directories the path prefix. (Seepath_resolution(7).)
(For UDP sockets) An attempt was made to send to a network/broadcast address as though it was a unicast address.
EAGAINorEWOULDBLOCKโ The socket is marked nonblocking and the requested operation would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities. check both, no skipping ๐EAGAINโ (Internet domain datagram sockets) The socket referred to bysockfdhad not previously been bound to an address and, upon attempting to bind it to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. See the discussion of/proc/sys/net/ipv4/ip_local_port_rangeinip(7).EALREADYโ Another Fast Open is in progress.EBADFโsockfdis not a valid open file descriptor.ECONNRESETโ Connection reset by peer.EDESTADDRREQโ The socket is not connection-mode, and no peer address is set.EFAULTโ An invalid user space address was specified for an argument.EINTRโ A signal occurred before any data was transmitted; seesignal(7).EINVALโ Invalid argument passed.EISCONNโ The connection-mode socket was connected already but a recipient was specified. (Now either this error is returned, or the recipient specification is ignored.)EMSGSIZEโ The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible.ENOBUFSโ The output queue for a network interface was full. This generally indicates that the interface has stopped sending, but may be caused by transient congestion. (Normally, this does not occur in Linux. Packets are just silently dropped when a device queue overflows.) ๐ENOMEMโ No memory available.ENOTCONNโ The socket is not connected, and no target has been given.ENOTSOCKโ The file descriptorsockfddoes not refer to a socket.EOPNOTSUPPโ Some bit in theflagsargument is inappropriate for the socket type. wrong flag for the wrong girl bestie ๐EPIPEโ The local end has been shut down on a connection oriented socket. In this case, the process will also receive aSIGPIPEunlessMSG_NOSIGNALis set.
๐ฐ๏ธ VERSIONS ๐ฐ๏ธ
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. glibc said "I'll do what I want" ๐
๐ STANDARDS ๐
POSIX.1-2024.
MSG_CONFIRM is a Linux extension.
๐ HISTORY (the lore, bestie)
Okay so this whole thing goes way back: 4.4BSD, SVr4, and POSIX.1-2001 โ and it first appeared in 4.2BSD. Like, it's a legit elder. Respect the lineage. ๐งโจ
And ngl, POSIX.1-2001 is kinda minimal about it โ it only says anything about the MSG_OOB and MSG_EOR flags. That's it. That's the whole list. ๐
Then POSIX.1-2008 rolls up and adds a specification of MSG_NOSIGNAL โ so that flag got its official moment. Slay for MSG_NOSIGNAL, honestly. ๐
๐๏ธ NOTES (the lil side quest)
If you're the kind of girl who wants to send multiple datagrams in a single call โ okay, efficient icon โ go check out sendmmsg(2). That's the Linux-specific system call for exactly that. One call, many datagrams. It's giving main character energy. ๐
๐ BUGS (the messy part, be aware)
Heads up bestie, this one's a real gotcha: Linux may returnEPIPEinstead ofENOTCONN.
So don't be out here hard-coding your error handling like the universe is consistent. It is not. Plan accordingly. ๐ญ
๐ EXAMPLES (show me the receipts)
There's a whole example of how to use sendto() over in getaddrinfo(3). So if you're sitting there like "okay but how do I actually call this thing", that's where the demo lives. Go look, it's cute. โจ
๐ฆ SEE ALSO (the group chat, aka the whole friend group)
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)
Okay that's the whole page, you made it to the end, I'm so proud of you ๐ This lil masterpiece is from Linux man-pages 6.20, dated 2025-10-29 โ so yeah, recent, fresh, iconic. Go forth and send(2) responsibly, bestie. Periodt. ๐
๐๐ซถ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1983-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms