Unix Network Programming Manual
Functions
The following functions are shown here:
/* ==============================================================> MakeAddr
*
* socket = MakeAddr (family, service, host, protocol)
*
* Create an internet address structure for the family, service, host and
* protocol.
*
* ======================================================================
*/
struct sockaddr_in *MakeAddr
( int family,
char *service,
char *host,
char *protocol
)
{
static struct sockaddr_in addr;
struct servent *servp;
struct hostent *hostp;
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
if (service == NULL)
addr . sin_port = 0;
else
{
if ((servp = getservbyname (service, protocol)) == NULL)
{
perror ("Attempting to get the service entry");
return (NULL);
}
addr . sin_port = servp -> s_port;
}
if (host == NULL)
addr.sin_addr.s_addr = htonl (INADDR_ANY);
else
{
if ((hostp = gethostbyname (host)) == NULL)
{
perror ("Attempting to get the host entry");
return (NULL);
}
addr.sin_addr.s_addr = ((struct in_addr *) (hostp -> h_addr)) -> s_addr;
}
return (&addr);
}
/* ===================================================================>
*
* socket = MakeSocket (type, addr)
*
* Create a socket of type type and bind it to *addr.
*
* ======================================================================
*/
int MakeSocket
( int type,
struct sockaddr_in *addr
)
{
int sock;
/*
* Build and bind the socket.
*/
sock = socket(addr -> sin_family, type,0);
if (sock == -1)
{ perror("opening socket");
return (-1);
}
if (bind(sock, addr, sizeof (struct sockaddr_in)) < 0)
{
perror("on bind");
return (-1);
}
return (sock);
}
Java