bind(2)
bind(2) โ bestie, we're giving your socket a NAME ๐
okay so real talk, this is straight out of the System Calls Manual section of your friendly neighborhood man pages, and it's about to be your new fave syscall. no cap. ๐ซถ
โจ NAME โจ
bind โ literally bind a name to a socket. that's it. that's the whole job. iconic behavior.
๐ LIBRARY ๐
Standard C library (libc, -lc) โ so you just link -lc like always and you're golden โจ
๐ธ SYNOPSIS ๐ธ
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
look at her. three little args. so tidy. ๐
๐ฆ DESCRIPTION ๐ฆ
okay so here's the tea. when you make a socket with socket(2), it shows up in a name space (aka an address family) but it has no address. it's giving... newborn. blank slate. main character with no backstory yet ๐ญ
bind() fixes that. it assigns the address that addr points to, to the socket that file descriptor sockfd refers to. and addrlen? that's the size, in bytes, of the address structure that addr is pointing at. simple as.
traditionally this whole operation is called โassigning a name to a socketโ โ which, honestly, is such a cute way to describe it. they ATE with that phrasing ๐ฅ
ngl, it's normally necessary to assign a local address with bind() before a SOCK_STREAM socket can receive connections (go peek at accept(2) if you need the full picture).
the rules for name binding? they vary between address families. so like, consult the manual entries in Section 7 for the detailed lore:
- for
AF_INET, seeip(7) - for
AF_INET6, seeipv6(7) - for
AF_UNIX, seeunix(7) - for
AF_APPLETALK, seeddp(7) - for
AF_PACKET, seepacket(7) - for
AF_X25, seex25(7) - for
AF_NETLINK, seenetlink(7)
kinda a whole roster, lowkey. ๐
the actual structure you pass for the addr argument depends on the address family. the sockaddr structure is defined as something like this:
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
and the only purpose of this structure โ listen up, this is the part people always get wrong โ is to cast the structure pointer passed in addr, purely to avoid compiler warnings. that's it!! it's a vibe-check struct. see the EXAMPLES below for how it actually goes down ๐
โญ RETURN VALUE โญ
- on success โ zero is returned. slay.
- on error โ -1 is returned, and
errnois set to tell you what went wrong ๐
๐ ERRORS ๐
here's the full drama list. every single one, no skips:
EACCESโ the address is protected, and the user is not the superuser. (root only, bestie ๐)EADDRINUSEโ the given address is already in use.EADDRINUSEโ (Internet domain sockets) the port number was specified as zero in the socket address structure, but, upon attempting to bind 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).EBADFโsockfdis not a valid file descriptor.EINVALโ the socket is already bound to an address.EINVALโaddrlenis wrong, oraddris not a valid address for this socket's domain.ENOTSOCKโ the file descriptorsockfddoes not refer to a socket.EADDRNOTAVAILโ a nonexistent interface was requested or the requested address was not local.
the following errors are specific to UNIX domain (AF_UNIX) sockets, so if you're in that lane, this is your section:
EACCESโ search permission is denied on a component of the path prefix. (see alsopath_resolution(7).)EFAULTโaddrpoints outside the user's accessible address space.ELOOPโ too many symbolic links were encountered in resolvingaddr.ENAMETOOLONGโaddris too long.ENOENTโ a component in the directory prefix of the socket pathname does not exist.ENOMEMโ insufficient kernel memory was available.ENOTDIRโ a component of the path prefix is not a directory.EROFSโ the socket inode would reside on a read-only filesystem.
๐ซถ also note: other errors may be generated by the underlying protocol modules. so like, it's not always gonna be one of these. keep an open mind, bestie.
๐ VERSIONS ๐
portable programs must ensure that addr.sun_path is a null-terminated string for AF_UNIX sockets. do not forget this. it WILL bite you. ๐ฆ
๐ STANDARDS ๐
POSIX.1-2024. periodt.
๐ HISTORY ๐
POSIX.1-2001, SVr4, 4.2BSD. she's been iconic for DECADES, we're just livin in her world ๐
๐ BUGS ๐
the transparent proxy options are not described. so, y'know. there's that. ๐
โจ EXAMPLES โจ
an example of the use of bind() with Internet domain sockets can be found in getaddrinfo(3) โ go look, it's cute.
the following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections:
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define MY_SOCK_PATH "/somepath"
#define LISTEN_BACKLOG 50
int
main(void)
{
int sfd, cfd;
socklen_t peer_addr_size;
struct sockaddr_un my_addr, peer_addr;
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1)
err(EXIT_FAILURE, "socket");
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, MY_SOCK_PATH,
sizeof(my_addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *) &my_addr,
sizeof(my_addr)) == -1)
err(EXIT_FAILURE, "bind");
if (listen(sfd, LISTEN_BACKLOG) == -1)
err(EXIT_FAILURE, "listen");
/* Now we can accept incoming connections one
at a time using accept(2). */
peer_addr_size = sizeof(peer_addr);
cfd = accept(sfd, (struct sockaddr *) &peer_addr,
&peer_addr_size);
if (cfd == -1)
err(EXIT_FAILURE, "accept");
/* Code to deal with incoming connection(s)... */
if (close(sfd) == -1)
err(EXIT_FAILURE, "close");
if (unlink(MY_SOCK_PATH) == -1)
err(EXIT_FAILURE, "unlink");
}
copy that whole thing, it's perfect, it's canon, it's the blueprint. ๐ซถ๐
๐ SEE ALSO ๐
accept(2), connect(2), getsockname(2), listen(2), socket(2), getaddrinfo(3), getifaddrs(3), ip(7), ipv6(7), path_resolution(7), socket(7), unix(7)
read them all. i'm not joking. bestie homework. ๐โจ
and that's the whole thing, gorgeous โ this page came to you from Linux man-pages 6.20, dated 2026-02-08, for bind(2). go forth and bind responsibly ๐
๐๐
๐ฆ 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: Linux-man-pages-copyleft
- Copyright 1993, Rickard E. Faith <faith@cs.unc.edu>
- Copyright 2005-2007, Michael Kerrisk <mtk.manpages@gmail.com>
- Copyright 1983, 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