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

Writing the Client Process

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

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

Creating a Socket

The client 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 client process should create sockets before requesting a connection. Refer to the socket(2) man page for more information on socket.

Requesting a Connection

Once the server process is listening for connection requests, the client process can request a connection with the connect call. connect 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:

connect(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 requesting connection

addr

pointer to the socket address

pointer to the socket address of the socket to which client wants to connect

addrlen

length of address

size of address structure pointed to by addr

Function result:

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

Example:

struct sockaddr_in peeraddr;
...
connect (s, &peeraddr, sizeof(struct sockaddr_in));

connect initiates a connection and blocks if the connection is not ready, unless you are using nonblocking I/O. When the connection is ready, the client process completes its connect call and the server process can complete its accept call.

NOTE: The client process does not get feedback that the server process has completed the accept call. As soon as the connect call returns, the client process can send data. Local internet and port addresses are bound when connect is executed if you have not already bound them yourself. These address values are chosen by the local host.

When to Request a Connection

The client process should request a connection after socket is created and after server socket has a listening socket. Refer to the connect(2) man page for more information on connect.

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