BSD Sockets Interface Programmer's Guide

32 Chapter 2
Using Internet Stream Sockets
Preparing Address Variables
The server process only needs an address for its own socket. Your client
process may not need an address for its local socket. Refer to the
inet(7f) man page for more information on sockaddr_in.
Getting the Remote Host's Internet Address
gethostbyname obtains the internet address of the host and the length
of that address (as the size of struct in_addr) from /etc/hosts or
from NIS name server. gethostbyname and its parameters are
described in the following table.
Include files: #include <netdb.h>
System call: struct hostent *gethostbyname(name)
char *name;
Function result: pointer to struct hostent containing internet address
NULL pointer (0) if failure occurs.
Example:
Field Description
short sin_family Specifies the address family and should
always be set to AF_INET.
u_short sin_port Specifies the port address. Assign this field
when you bind the port address for the
socket or when you get a port address for a
specific service.
struct inaddr
sin_addr
Specifies the internet address. Assign this
field when you get the internet address for
the remote host.
Parameter Description of Contents INPUT Value
name pointer to a valid host name
(null-terminated string)
host name string