5 bind - bind a name to a socket
8 #include <network.h>
10 int bind(int sockfd, struct sockaddr *my_addr, int
14 bind gives the socket sockfd the local address my_addr.
15 my_addr is addrlen bytes long. Traditionally, this is
16 called "assigning a name to a socket." (When a socket is
17 created with socket(2), it exists in a name space (address
18 family) but has no name assigned.)
20 Before a SOCK_STREAM socket is put into the LISTEN state
21 to receive connections, you usually need to first assign a
22 local address using bind to make the socket visible.
25 Binding a name that is not in the abstract namespace in
26 the UNIX domain creates a socket in the file system that
27 must be deleted by the caller when it is no longer needed
30 The rules used in name binding vary between communication
31 domains. Consult the manual entries in section 4 for
32 detailed information. For IP see ip(4) and for PF_UNIX see
33 unix(4). If you want to listen to every local interface
34 for IPv4 set the sin_addr member of the IP-specific sock-
35 addr_in to INADDR_ANY. For IP only one socket may be
36 bound to a specific local address/port pair. For TCP a
37 bound local socket endpoint (address/port pair) is
38 unavailable for some time after closing the socket, unless
39 the SO_REUSEADDR flag is set. Note that carelessly setting
40 SO_REUSEADDR might make TCP more unreliable unless PAWS is
41 used (see tcp(4)); the delay is needed to handle old pack-
42 ets still in the network.
44 IP sockets may also bind to a broadcast or multicast
48 On success, zero is returned. On error, -1 is returned,
49 and errno is set appropriately.
52 EBADF sockfd is not a valid descriptor.
54 EINVAL The socket is already bound to an address. This
55 may change in the future: see linux/unix/sock.c
59 Argument is a descriptor for a file, not a socket.