Added mention of AF_INET6 address family.

Added discussion of sockaddr structure and an example in the Unix domain.
This commit is contained in:
Michael Kerrisk 2005-12-14 11:37:28 +00:00
parent a917b0bb2c
commit ffa01655e5
1 changed files with 57 additions and 1 deletions

View File

@ -101,6 +101,10 @@ the manual entries in Section 7 for detailed information. For
see see
.BR ip (7), .BR ip (7),
for for
.B AF_INET6
see
.BR ipv6 (7),
for
.B AF_UNIX .B AF_UNIX
see see
.BR unix (7), .BR unix (7),
@ -121,6 +125,56 @@ and for
see see
.BR netlink (7). .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" .SH "RETURN VALUE"
On success, zero is returned. On error, \-1 is returned, and On success, zero is returned. On error, \-1 is returned, and
.I errno .I errno
@ -218,4 +272,6 @@ See also
.BR socket (2), .BR socket (2),
.BR getaddrinfo (3), .BR getaddrinfo (3),
.BR ip (7), .BR ip (7),
.BR socket (7) .BR ipv6 (7),
.BR socket (7),
.BR unix (7)