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

#include <netdb.h>
struct hostent *hp; /* pointer to host info for remote host */
...
peeraddr.sin_family = AF_INET;
hp = gethostbyname (argv[1]);
peeraddr_in.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 server process is preparing to offer a service, it must get the port
address for the service from /etc/services so it can bind that address
to its "listen" socket. If the service is not already in /etc/services, you
must add it.
When a client process needs to use a service that is offered by some
server process, it must request a connection to that server process's
"listening" socket. The client process must know the port address for that
socket.
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:
Chapter 2
#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.
Using Internet Stream Sockets
Preparing Address Variables
INPUT Value
service name
tcp or 0 if TCP is the
only protocol for the
service
33

Advertisement

Table of Contents
loading

Table of Contents