HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 4 Using Internet Datagram Sockets

Preparing Address Variables

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

Before your client process can make a request of the server process, you must establish the correct variables and collect the information that you need about the server process and the service provided.

The server process needs to:

  • Declare socket address variables.

  • Assign a wildcard address.

  • Get the port address of the service that you want to provide.

The client process needs to:

  • Declare socket address variables.

  • Get the remote server's internet address.

  • Get the port address for the service that you want to use.

These activities are described next. Refer to the program example at the end of this chapter to see how these activities work together.

Declaring Socket Address Variables

You need to declare a variable of type struct sockaddr_in to use the local socket address for both processes. For example, the following declarations are used in the example client program:

struct sockaddr_in myaddr; /* for local socket address  */
struct sockaddr_in servaddr; /* for server socket address */

sockaddr_in is a special case of sockaddr and is used with the AF_INET addressing domain. Both types are shown in this chapter, but sockaddr_in makes it easier to manipulate the internet and port addresses. Some of the BSD Sockets system calls are declared using a pointer to sockaddr, but you can also use a pointer to sockaddr_in.

The sockaddr_in address structure consists of the following fields:

FieldDescription
short sin_familySpecifies the address family and should always be set to AF_INET.
u_short sin_portSpecifies 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_addrSpecifies the internet address. Assign this field when you get the internet address for the remote host.

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;

Parameter

Description of Contents

INPUT Value

name

pointer to a valid node name (null-terminated string)

host name

Function result:

pointer to struct hostent containing internet address, NULL pointer (0) if failure occurs.

Example:
         
#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:

#include <netdb.h>

System call:

struct servent *getservbyname(name, proto) 
char *name, *proto;

Parameter

Description of Contents

INPUT Value

name

pointer to a valid service name

service name

proto

pointer to the protocol to be used

udp or 0 if UDP is the only protocol for the service

Function result:

pointer to struct servent containing port address, NULL pointer (0) if failure occurs.

Example:
#include <netdb.h>
struct servent *sp; /* pointer to service info */
...
sp = getservbyname ("example", "udp");
servaddr.sin_port = sp->s_port;

When to Get Server's Socket Address

The server process should get the server's socket address before binding. The client process should get the server's socket address before client requests the service from the host. Refer to the getservent(3N) man page for more information on getservbyname.

Using a Wildcard Local Address

Wildcard addressing simplifies local address binding. When an address is assigned the value of INADDR_ANY, the host interprets the address as any valid address. This means that the server process can receive on a wildcard address and does not have to look up its own internet address.

For example, to bind a specific port address to a socket, but leave the local internet address unspecified, the following source code could be used:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
...
struct sockaddr_in sin;
...
s = socket(AF_INET, SOCK_DGRAM, 0);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = MYPORT;
bind (s, &sin, sizeof(sin));
© 1997 Hewlett-Packard Development Company, L.P.