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

Writing the Server and Client Processes

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

This section explains the calls your server and client processes must make.

Creating Sockets

Both processes must call socket to create communication endpoints. socket and its parameters are described in the following table.

Include files:

#include <sys/types.h> 
#include <sys/socket.h>

System call:

s = socket(af, type, protocol) 
int s, af, type, protocol;

Parameter

Description of Contents

INPUT Value

af

address family

AF_INET

type

socket type

SOCK_DGRAM

protocol

underlying protocol to be used

0 (default) or value returned by getprotobyname

Function result:

socket number (HP-UX file descriptor), -1 if failure occurs.

Example:
ls = socket (AF_INET, SOCK_DGRAM, 0);

The socket number returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard file system calls. A socket descriptor is treated like a file descriptor for an open file.

NOTE: To use write(2) with a datagram socket, you must declare a default address. Refer to the "Specifying a Default Socket Address" section of the "Advanced Topics for Internet Datagram Sockets" chapter for more information.

When to Create Sockets

The server or client process should create a socket before any other BSD Sockets system calls. Refer to the socket(2) man page for more information on socket.

Binding Socket Addresses to Datagram Sockets

After each process has created a socket, it must call bind to bind a socket address. Until an address is bound, other processes have no way to reference it.

The server process must bind a specific port address to its socket. Otherwise, a client process would not know what port to send requests to for the desired service.

The client process can let the local host bind its local port address. The client does not need to know its own port address, and if the server process needs to send a reply to the client's request, the server can find out the client's port address when it receives with recvfrom.

Set up the address structure with a local address before you make a bind call. Use the wildcard address so your processes do not have to look up their own internet addresses. bind and its parameters are described in the following table.

Include files:

#include <sys/types.h> 
#include <netinet/in.h>
#include <sys/socket.h>

System call:

bind (s, addr, addrlen) 
int s;
struct sockaddr *addr;
int addrlen;

Parameter

Description of Contents

INPUT Value

s

socket descriptor of local socket

socket descriptor of socket to be bound

addr

socket address

pointer to address to be bound to s

addrlen

length of socket address

size of struct sockaddr_in_address

Function result:

0 if bind is successful, -1 if failure occurs.

Example:
struct sockaddr_in myaddr;
...
bind (s, &myaddr, sizeof(struct sockaddr_in));

When to Bind Socket Addresses

The client and server process should bind socket addresses after the socket is created and before any other BSD Sockets system calls. Refer to the bind(2) man page for more information on bind.

© 1997 Hewlett-Packard Development Company, L.P.