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