BSD Sockets Interface Programmer's Guide

Chapter 4 89
Using Internet Datagram Sockets
Preparing Address Variables
The server process must bind the port address of the service to its own
socket and establish an address structure to store the clients' addresses
when they are received with recvfrom. The client process does not have
to bind a port address for its local socket; the host binds one
automatically if one is not already bound. Refer to the inet(7F) man
page for more information on sockaddr_in.
Getting the Remote Host's Network Address
The client process can use gethostbyname to obtain the internet
address of the host and the length of that address (as the size of struct
inaddr) from /etc/hosts, NIS, or BIND. 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 in_addr
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 node name
(null-terminated string)
host name