HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 7 Using UNIX Domain Datagram Sockets

Writing the Server and Client Processes

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

This section discusses 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 af, type, protocol;

Parameter

Description of Contents

INPUT Value

af

address family

AF_UNIX

type

socket type

SOCK_DGRAM

protocol

underlying protocol to be used

0 (default)

Function result:

socket number (HP-UX file descriptor) if successful, -1 if socket call fails.

Example:
#include <sys/type.h>
#include <sys/socket.h>
...
s = socket(AF_UNIX, SOCK_DGRAM, 0)

When to Create Sockets

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

Binding Socket Addresses to UNIX Domain Datagram Sockets

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

The server process must bind a specific pathname to its socket. Set up the address structure with a local address before the server makes a call to bind.

The bind system call creates the inode file. If the inode file is not deallocated after bound sockets are closed, the names will continue to exist and cause directories to fill up with the unused files. To avoid directories filling up with these unused files, you can remove the files by calling unlink or remove them manually with the rm command. bind and its parameters are described in the following table.

Include files:

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

System call:

bind(s, addr, addrlen); 
int s;
struct sockaddr_un *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_un address

Function result:

0 if bind is successful, -1 if bind fails.

Example:
#include <sys/type.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCKET_PATH "/tmp/myserver"
struct sockaddr_un servaddr;
...
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, SOCKET_PATH);
unlink(SOCKET_PATH);
bind(s, &servaddr, sizeof(struct sockaddr_un));

When to Bind Socket Addresses

The server process should bind socket addresses after 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.