1BIND(2)                    Linux Programmer's Manual                   BIND(2)
2
3
4

NAME

6       bind - bind a name to a socket
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/socket.h>
11
12       int   bind(int   sockfd,  const  struct  sockaddr  *my_addr,  socklen_t
13       addrlen);
14

DESCRIPTION

16       bind() gives the socket sockfd the local address my_addr.   my_addr  is
17       addrlen bytes long.  Traditionally, this is called “assigning a name to
18       a socket.”  When a socket is created with socket(2),  it  exists  in  a
19       name space (address family) but has no name assigned.
20
21       It  is normally necessary to assign a local address using bind() before
22       a SOCK_STREAM socket may receive connections (see accept(2)).
23
24       The rules used in name binding vary between address families.   Consult
25       the  manual  entries in Section 7 for detailed information. For AF_INET
26       see ip(7), for AF_INET6 see  ipv6(7),  for  AF_UNIX  see  unix(7),  for
27       AF_APPLETALK  see  ddp(7),  for AF_PACKET see packet(7), for AF_X25 see
28       x25(7) and for AF_NETLINK see netlink(7).
29
30       The actual structure passed for the my_addr argument will depend on the
31       address family.  The sockaddr structure is defined as something like:
32
33         struct sockaddr {
34             sa_family_t sa_family;
35             char        sa_data[14];
36         }
37
38       The  only  purpose  of  this structure is to cast the structure pointer
39       passed in my_addr in order to avoid compiler warnings.   The  following
40       example  shows  how  this  is  done  when  binding a socket in the Unix
41       (AF_UNIX) domain:
42
43         #include <sys/socket.h>
44         #include <sys/un.h>
45         #include <stdlib.h>
46         #include <stdio.h>
47         #include <string.h>
48
49         #define MY_SOCK_PATH "/somepath"
50
51         int
52         main(int argc, char *argv[])
53         {
54             int sfd;
55             struct sockaddr_un addr;
56
57             sfd = socket(AF_UNIX, SOCK_STREAM, 0);
58             if (sfd == -1) {
59                 perror("socket");
60                 exit(EXIT_FAILURE);
61             }
62
63             memset(&addr, 0, sizeof(struct sockaddr_un));
64                                 /* Clear structure */
65             addr.sun_family = AF_UNIX;
66             strncpy(addr.sun_path, MY_SOCK_PATH,
67                     sizeof(addr.sun_path) - 1);
68
69             if (bind(sfd, (struct sockaddr *) &addr,
70                     sizeof(struct sockaddr_un)) == -1) {
71                 perror("bind");
72                 exit(EXIT_FAILURE);
73             }
74             ...
75         }
76

RETURN VALUE

78       On success, zero is returned.  On error, -1 is returned, and  errno  is
79       set appropriately.
80

ERRORS

82       EACCES The address is protected, and the user is not the superuser.
83
84       EADDRINUSE
85              The given address is already in use.
86
87       EBADF  sockfd is not a valid descriptor.
88
89       EINVAL The socket is already bound to an address.
90
91       ENOTSOCK
92              sockfd is a descriptor for a file, not a socket.
93
94       The following errors are specific to UNIX domain (AF_UNIX) sockets:
95
96       EACCES Search  permission  is denied on a component of the path prefix.
97              (See also path_resolution(2).)
98
99       EADDRNOTAVAIL
100              A non-existent interface was requested or the requested  address
101              was not local.
102
103       EFAULT my_addr points outside the user's accessible address space.
104
105       EINVAL The  addrlen is wrong, or the socket was not in the AF_UNIX fam‐
106              ily.
107
108       ELOOP  Too many symbolic links were encountered in resolving my_addr.
109
110       ENAMETOOLONG
111              my_addr is too long.
112
113       ENOENT The file does not exist.
114
115       ENOMEM Insufficient kernel memory was available.
116
117       ENOTDIR
118              A component of the path prefix is not a directory.
119
120       EROFS  The socket inode would reside on a read-only file system.
121

BUGS

123       The transparent proxy options are not described.
124

CONFORMING TO

126       SVr4, 4.4BSD, POSIX.1-2001  (the  bind()  function  first  appeared  in
127       4.2BSD).
128

NOTE

130       The third argument of bind() is in reality an int (and this is what 4.x
131       BSD and libc4 and libc5 have).  Some POSIX confusion  resulted  in  the
132       present socklen_t, also used by glibc.  See also accept(2).
133

SEE ALSO

135       accept(2),  connect(2),  getsockname(2), listen(2), path_resolution(2),
136       socket(2), getaddrinfo(3), ip(7), ipv6(7), socket(7), unix(7)
137
138
139
140Linux 2.6.7                       2004-06-23                           BIND(2)
Impressum