|
|
HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 4 Using Internet Datagram SocketsPreparing Address Variables |
|
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:
The client process needs to:
These activities are described next. Refer to the program example at the end of this chapter to see how these activities work together. 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:
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: 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. 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.
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. 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
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:
|
|