|
|
The two most common addressing schemes are
AF_UNIX and AF_INET
(Address Family UNIX and
Address Family Internet).
The sockaddr structure contains structure members that
specify the protocol (address) family and the actual address.
struct sockaddr {
u_short sa_family; /*address family*/
char sa_data[14]; /*up to 14 bytes of address*/
};
AF_UNIX addressing uses UNIX
system pathnames to identify sockets.
Such sockets are very useful
for interprocess communication (IPC)
between processes on the same machine.
AF_INET addresses consist of four-byte numbers.
When these addresses are to be read by people, they are
usually written as four decimal numbers separated by
periods (for example: 192.9.200.10).
There is also a port number to
allow more than one socket on each machine.
The sockaddr_in structure is:
struct sockaddr_in {
short sin_family; /* address family*/
u_short sin_port; /* 2 bytes port number */
struct in_addr sin_addr; /* 4 bytes IP address */
char sin_data[8]; /* unused */
};
The
gethostbyname(SLIB)
call is used to find the Internet address of a host. The call takes
the form:
hostent = gethostbyname(name)where name is the name of a machine. The call returns a pointer to an object with the following structure containing the address of the machine.
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length in bytes of address */
char **h_addr_list; /* address list from name server */
};
The structure member h_name contains the official,
qualified name of the host. h_alias contains a
null-terminated list of alternate names for the host.
The protocol family of the address is specified in
h_addrtype,
and h_length contains the address length.
h_addr_list is a null-terminated array of addresses for the host.
Conversely, the
gethostbyaddr(SLIB)
call is used to get the name of a machine.