bind(3)

NAME
bind - bind an address to a socket

SYNOPSIS
#include <sys/socket.h>

  AF_UNIX only:
#include <sys/un.h>

int bind(int s, struct sockaddr *addr, size_t addrlen);

DESCRIPTION
bind assigns an address to an unbound socket. When a socket is created with socket(3), it exists in an address space (address family) but has no address assigned. bind causes the socket whose descriptor is s to become bound to the address specified in the socket address structure pointed to by addr.

addrlen must specify the size of the address structure. Since the size of the socket address structure varies between socket address families, the correct socket address structure should be used with each address family (for example, struct sockaddr_un for AF_UNIX). Typically, the sizeof function is used to pass this value in the bind call (for example, sizeof(struct sockaddr_un)).

The rules used in address binding vary between communication domains. For example, when binding an AF_UNIX socket to a path name (such as /tmp/mysocket), an open file having that name is created in the file system. When the bound socket is closed, that file still exists unless it is removed or unlinked.

RETURN VALUE
If the bind is successful, a 0 value is returned. If it fails, -1 is returned and an error code is stored in errno.

DIAGNOSTICS
bind fails if:

[EBADF]
    s is not a valid descriptor.

[EADDRINUSE]
    The specified address is already in use.

[EINVAL]
    The socket is already bound to an address, the socket has been shut down or addrlen is a bad value. s is not a socket.

[EAFNOSUPPORT]
    Requested address does not match the address family of this socket.

[EACCES]
    The user has no write permission for the directory containing the socket file. (AF_UNIX only.)

[EDESTADDRREQ]
    No addr parameter was specified.

SEE ALSO
connect(3), getsockname(3), listen(3), socket(3).