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:

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 errno is 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_range in ip(7).
  • EBADF โ€” sockfd is not a valid file descriptor.
  • EINVAL โ€” the socket is already bound to an address.
  • EINVAL โ€” addrlen is wrong, or addr is not a valid address for this socket's domain.
  • ENOTSOCK โ€” the file descriptor sockfd does 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 also path_resolution(7).)
  • EFAULT โ€” addr points outside the user's accessible address space.
  • ELOOP โ€” too many symbolic links were encountered in resolving addr.
  • ENAMETOOLONG โ€” addr is 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms