Getting The Port Address For The Desired Service - HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

Using Internet Datagram Sockets
Preparing Address Variables
#include <netdb.h>
struct hostent *hp; /* point to host info for name server host */
...
servaddr.sin_family = AF_INET;
hp = gethostbyname (argv[1]);
servaddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
The argv[1] parameter is the host name specified in the client program
command line. Refer to the gethostent(3N) man page for more
information on gethostbyname.
Getting the Port Address for the Desired
Service
When a client process needs to use a service that is offered by some
server process, it must send a message to the server's socket. The client
process must know the port address for that socket. If the service is not
in /etc/services, you must add it.
getservbyname obtains the port address of the specified service from
/etc/services. getservbyname and its parameters are described in
the following table.
Include files:
System call:
Function result: pointer to struct servent containing port address,
Example:
#include <netdb.h>
struct servent *sp; /* pointer to service info */
...
sp = getservbyname ("example", "udp");
servaddr.sin_port = sp->s_port;
90
#include <netdb.h>
struct servent *getservbyname(name, proto)
char *name, *proto;
Parameter
Description of Contents
name
pointer to a valid service
name
proto
pointer to the protocol to be
used
NULL pointer (0) if failure occurs.
INPUT Value
service name
udp or 0 if UDP is the
only protocol for the
service
Chapter 4

Advertisement

Table of Contents
loading

Table of Contents