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

Writing the Server Process

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

This section explains the calls your server process must make to connect with and serve a client process.

Creating a Socket

The server process must call socket to create a communication endpoint. 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_INET

type

socket type

SOCK_STREAM

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:

s = socket (AF_INET, SOCK_STREAM, 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 after a BSD Sockets connection is established. A socket descriptor is treated like a file descriptor for an open file.

When to Create Sockets

The server 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 a Socket Address to the Server Process's Socket

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

The server process must bind a specific port address to this socket, which is used for listening. Otherwise, a client process would not know what port to connect to for the desired service.

Set up the address structure with a local address before you make a bind call. Use a wildcard address so your server process does not have to look up its own internet address. 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

Function result:

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

Example:

struct sockaddr_in myaddr;
...
bind (ls, &myaddr, sizeof(struct sockaddr_in));

When to Bind Socket Addresses

The server process should bind the socket address 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.

Setting Up the Server to Wait for Connection Requests

Once your server process has an address bound to it, it must call listen to set up a queue that accepts incoming connection requests. The server process then monitors the queue for requests (using select(2) or accept ). The server process cannot respond to a connection request until it has executed listen. listen and its parameters are described in the following table.

Include files:

none

System call:

listen(s, backlog) 
int s, backlog;

Parameter

Description of Contents

INPUT Value

s

socket descriptor of local socket

server socket's descriptor

backlog

preferred maximum number of connection requests in the queue at any time

size of queue (between 0 and SOMAXCONN)

Function result:

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

Example:

listen (ls, 5);

backlog is the preferred number of unaccepted incoming connections allowed at a given time. The actual number may be greater than the specified backlog. When the request is full, further connection requests are rejected.

A backlog of 0 specifies only 1 pending connection can exist at any given time. SOMAXCONN is defined in <sys/socket.h>. The default setting is 20.

When to Set Up Server to Listen

The server process should be set up to listen after socket is created and bound and before the server can respond to connection requests. Refer to the listen(2) man page for more information on listen.

Accepting a Connection

The server process can accept any connection requests that enter its queue after it executes listen. accept creates a new socket for the connection and returns the socket descriptor for the new socket. The new socket:

  • Is created with the same properties as the old socket.

  • Has the same bound port address as the old socket.

  • Is connected to the client process's socket.

accept blocks until there is a connection request from a client process in the queue, unless you are using nonblocking I/O. accept 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:

s = accept(ls,addr,addrlen) 
int s;
int ls;
struct sockaddr *addr;
int *addrlen;

Parameter

Contents

INPUT Value

OUTPUT Value

ls

socket descriptor of local socket

socket descriptor of server socket

unchanged

addr

socket address

pointer to address structure where address will be put

pointer to socket address of client socket that server's new socket is connected to

addrlen

length of address

pointer to the size of struct sockaddr_in

pointer to the actual length of address returned in addr

Function result:

socket descriptor of new socket if accept is successful, -1 if failure occurs.

Example:

struct sockaddr_in peeraddr;
...
addrlen = sizeof(sockaddr_in);
s = accept (ls, &peeraddr, &addrlen);

There is no way for the server process to indicate which requests it can accept. It must accept all requests or none. Your server process can keep track of which process a connection request is from by examining the address returned by accept. Once you have this address, you can use gethostbyaddr to get the hostname. You can close down the connection if you do not want the server process to communicate with that particular client host or port.

When to Accept a Connection

The server process should accept a connection after executing the listen call. Refer to the accept(2) man page for more information on accept.

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