bind(2)
Requires Optional LAN/X.25 Software
NAME
bind − bind an address to a socket
SYNOPSIS
#include <sys/socket.h>
AF_INET only:
#include <netinet/in.h>
AF_UNIX only:
#include <sys/un.h>
AF_CCITT only:
#include <x25/x25addrstr.h>
int bind(int s, const void ∗addr, int addrlen);
DESCRIPTION
bind assigns an address to an unbound socket. When a socket is created with socket(2), 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_in for AF_INET, and 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_in)).
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. When binding an AF_INET socket, sin_port can be a port number, or it can be zero. If sin_port is zero, the system assigns an unused port number automatically.
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.
[ENOTSOCK] s is not a socket.
[EADDRNOTAVAIL] The specified address is bad or not available from the local machine, or for AF_CCITT sockets which use “wild card” addressing, the specified address space overlays the address space of an existing bind.
[EADDRINUSE] The specified address is already in use.
[EINVAL] The socket is already bound to an address, the socket has been shut down, addrlen is a bad value, or an attempt was made to bind() an AF_UNIX socket to an NFS-mounted (remote) name.
AF_CCITT: The protocol- ID length is negative or greater than 8, or the X.121 address string contains an illegal character, or the X.121 address string is greater than 15 digits long.
[EAFNOSUPPORT] Requested address does not match the address family of this socket.
[EACCES] The requested address is protected, and the current user has inadequate permission to access it. (This error can be returned by AF_INET only.)
[EFAULT] addr is not a valid pointer.
[EOPNOTSUPP] The socket whose descriptor is s is of a type that does not support address binding.
[ENOBUFS] Insufficient buffer memory is available. The bind cannot complete.
[ENETUNREACH] The X.25 Level 2 protocol is down. The X.25 link is not working: wires might be broken, or connections are loose on the interface hoods at the modem, or the modem failed, or noise interfered with the line for an extremely long period of time.
[EDESTADDREQ] No addr parameter was specified.
[ENODEV] The x25ifname field name specifies a non-existent interface. (This error can be returned by AF_CCITT only.)
[ENETDOWN] The x25ifname field name specifies an interface that was shut down, or never initialized, or whose Level 2 protocol indicates that the link is not working: wires might be broken, the interface hoods on the modem are broken, the modem failed, the phone connection failed (this error can be returned by AF_CCITT only), noise interfered with the line for a long period of time.
AUTHOR
bind was developed by the University of California, Berkeley)
SEE ALSO
connect(2), getsockname(2), listen(2), socket(2), af_ccitt(7F), inet(7F), socketx25(7), tcp(7P), udp(7P), unix(7P).
Hewlett-Packard Company — HP-UX Release 8.05: June 1991