mirror of https://github.com/mkerrisk/man-pages
Added mention of AF_INET6 address family.
Added discussion of sockaddr structure and an example in the Unix domain.
This commit is contained in:
parent
a917b0bb2c
commit
ffa01655e5
58
man2/bind.2
58
man2/bind.2
|
@ -101,6 +101,10 @@ the manual entries in Section 7 for detailed information. For
|
|||
see
|
||||
.BR ip (7),
|
||||
for
|
||||
.B AF_INET6
|
||||
see
|
||||
.BR ipv6 (7),
|
||||
for
|
||||
.B AF_UNIX
|
||||
see
|
||||
.BR unix (7),
|
||||
|
@ -121,6 +125,56 @@ and for
|
|||
see
|
||||
.BR netlink (7).
|
||||
|
||||
The actual structure passed for the
|
||||
.I my_addr
|
||||
argument will depend on the address family.
|
||||
The
|
||||
.I sockaddr
|
||||
structure is defined as something like:
|
||||
.in +0.25in
|
||||
.nf
|
||||
|
||||
struct sockaddr {
|
||||
sa_family_t sa_family;
|
||||
char sa_data[14];
|
||||
}
|
||||
|
||||
.fi
|
||||
.in -0.25in
|
||||
The only purpose of this structure is to cast the structure
|
||||
pointer passed in
|
||||
.I my_addr
|
||||
in order to avoid compiler warnings.
|
||||
The following example shows how this is done when binding a socket
|
||||
in the Unix
|
||||
.RB ( AF_UNIX )
|
||||
domain:
|
||||
|
||||
.in +0.25in
|
||||
.nf
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
int sfd;
|
||||
struct sockaddr_un addr;
|
||||
|
||||
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sfd == -1) { perror("socket"); exit(EXIT_FAILURE); }
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||
/* Clear structure */
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, MY_SOCK_PATH,
|
||||
sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (bind(sfd, (struct sockaddr *) &addr,
|
||||
sizeof(struct sockaddr_un)) == -1) {
|
||||
perror("bind");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
.fi
|
||||
.in -0.25in
|
||||
.SH "RETURN VALUE"
|
||||
On success, zero is returned. On error, \-1 is returned, and
|
||||
.I errno
|
||||
|
@ -218,4 +272,6 @@ See also
|
|||
.BR socket (2),
|
||||
.BR getaddrinfo (3),
|
||||
.BR ip (7),
|
||||
.BR socket (7)
|
||||
.BR ipv6 (7),
|
||||
.BR socket (7),
|
||||
.BR unix (7)
|
||||
|
|
Loading…
Reference in New Issue