1BIND(2) Linux Programmer's Manual BIND(2)
2
3
4
6 bind - bind a name to a socket
7
9 #include <sys/socket.h>
10
11 int bind(int sockfd, const struct sockaddr *addr,
12 socklen_t addrlen);
13
15 When a socket is created with socket(2), it exists in a name space (ad‐
16 dress family) but has no address assigned to it. bind() assigns the
17 address specified by addr to the socket referred to by the file de‐
18 scriptor sockfd. addrlen specifies the size, in bytes, of the address
19 structure pointed to by addr. Traditionally, this operation is called
20 “assigning a name to a socket”.
21
22 It is normally necessary to assign a local address using bind() before
23 a SOCK_STREAM socket may receive connections (see accept(2)).
24
25 The rules used in name binding vary between address families. Consult
26 the manual entries in Section 7 for detailed information. For AF_INET,
27 see ip(7); for AF_INET6, see ipv6(7); for AF_UNIX, see unix(7); for
28 AF_APPLETALK, see ddp(7); for AF_PACKET, see packet(7); for AF_X25, see
29 x25(7); and for AF_NETLINK, see netlink(7).
30
31 The actual structure passed for the addr argument will depend on the
32 address family. The sockaddr structure is defined as something like:
33
34 struct sockaddr {
35 sa_family_t sa_family;
36 char sa_data[14];
37 }
38
39 The only purpose of this structure is to cast the structure pointer
40 passed in addr in order to avoid compiler warnings. See EXAMPLES be‐
41 low.
42
44 On success, zero is returned. On error, -1 is returned, and errno is
45 set to indicate the error.
46
48 EACCES The address is protected, and the user is not the superuser.
49
50 EADDRINUSE
51 The given address is already in use.
52
53 EADDRINUSE
54 (Internet domain sockets) The port number was specified as zero
55 in the socket address structure, but, upon attempting to bind to
56 an ephemeral port, it was determined that all port numbers in
57 the ephemeral port range are currently in use. See the discus‐
58 sion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
59
60 EBADF sockfd is not a valid file descriptor.
61
62 EINVAL The socket is already bound to an address.
63
64 EINVAL addrlen is wrong, or addr is not a valid address for this
65 socket's domain.
66
67 ENOTSOCK
68 The file descriptor sockfd does not refer to a socket.
69
70 The following errors are specific to UNIX domain (AF_UNIX) sockets:
71
72 EACCES Search permission is denied on a component of the path prefix.
73 (See also path_resolution(7).)
74
75 EADDRNOTAVAIL
76 A nonexistent interface was requested or the requested address
77 was not local.
78
79 EFAULT addr points outside the user's accessible address space.
80
81 ELOOP Too many symbolic links were encountered in resolving addr.
82
83 ENAMETOOLONG
84 addr is too long.
85
86 ENOENT A component in the directory prefix of the socket pathname does
87 not exist.
88
89 ENOMEM Insufficient kernel memory was available.
90
91 ENOTDIR
92 A component of the path prefix is not a directory.
93
94 EROFS The socket inode would reside on a read-only filesystem.
95
97 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (bind() first appeared in
98 4.2BSD).
99
101 For background on the socklen_t type, see accept(2).
102
104 The transparent proxy options are not described.
105
107 An example of the use of bind() with Internet domain sockets can be
108 found in getaddrinfo(3).
109
110 The following example shows how to bind a stream socket in the UNIX
111 (AF_UNIX) domain, and accept connections:
112
113 #include <sys/socket.h>
114 #include <sys/un.h>
115 #include <stdlib.h>
116 #include <stdio.h>
117 #include <string.h>
118
119 #define MY_SOCK_PATH "/somepath"
120 #define LISTEN_BACKLOG 50
121
122 #define handle_error(msg) \
123 do { perror(msg); exit(EXIT_FAILURE); } while (0)
124
125 int
126 main(int argc, char *argv[])
127 {
128 int sfd, cfd;
129 struct sockaddr_un my_addr, peer_addr;
130 socklen_t peer_addr_size;
131
132 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
133 if (sfd == -1)
134 handle_error("socket");
135
136 memset(&my_addr, 0, sizeof(my_addr));
137 my_addr.sun_family = AF_UNIX;
138 strncpy(my_addr.sun_path, MY_SOCK_PATH,
139 sizeof(my_addr.sun_path) - 1);
140
141 if (bind(sfd, (struct sockaddr *) &my_addr,
142 sizeof(my_addr)) == -1)
143 handle_error("bind");
144
145 if (listen(sfd, LISTEN_BACKLOG) == -1)
146 handle_error("listen");
147
148 /* Now we can accept incoming connections one
149 at a time using accept(2). */
150
151 peer_addr_size = sizeof(peer_addr);
152 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
153 &peer_addr_size);
154 if (cfd == -1)
155 handle_error("accept");
156
157 /* Code to deal with incoming connection(s)... */
158
159 /* When no longer required, the socket pathname, MY_SOCK_PATH
160 should be deleted using unlink(2) or remove(3). */
161 }
162
164 accept(2), connect(2), getsockname(2), listen(2), socket(2), getad‐
165 drinfo(3), getifaddrs(3), ip(7), ipv6(7), path_resolution(7),
166 socket(7), unix(7)
167
169 This page is part of release 5.13 of the Linux man-pages project. A
170 description of the project, information about reporting bugs, and the
171 latest version of this page, can be found at
172 https://www.kernel.org/doc/man-pages/.
173
174
175
176Linux 2021-03-22 BIND(2)