The BSD UNIX Socket Library |
The following scenario depicts a typical sequence
of events in a network application using stream sockets:
-
A server application issues a
socket
call, which returns an integer that is
the socket descriptor, allocated to the AF_INET addressing family.
-
The server uses the
bind
function to bind a name to the socket and allow the network access
to the socket.
-
The server issues the
listen
call to signal the network that it is ready to accept connections
from other applications.
-
The server issues an
accept
call to accept the connection request.
-
A client application issues a
connect
call
to the server.
-
The client and server conduct read/write operations.
-
The socket is disconnected with a
close
call.
Datagram sockets do not have to be bound or connected,
but they normally need to acquire additional address information in order
to communicate with each other.
The following sections describe the socket functions
that gather information, convert data, and accompany each step in the process
of communicating between applications over a network. Refer to Socket Function Reference for a complete description of
each socket function.
The complexity of network communications frequently
requires that an application have some means of determining the location of
sockets or other applications and of acquiring information about the network.
A
process may need to discover the address to
which the socket is connected. The process may also need to discover its
socket's local address. The following function calls determine socket addresses:
The
following function calls return the host's assigned name
and the host's Internet address:
The following socket
functions perform a translation between 32-bit IP addresses and the standard
network dotted decimal notation, or they divide or reassemble the network
and host sections of 32-bit IP addresses:
An application can obtain information about a host by submitting
either the host's name or its address. The following functions return the
host name and IP address:
An application can also obtain
information about a network. The following function calls return network names
and addresses:
An application can also obtain information about protocols.
The following function calls return protocol names and numbers:
An application can obtain information
about network services and their protocol ports. The following functions
return network service names and ports:
There
are three sets of library routines that access a database to return information
such as the names of machines and network services, and protocol port numbers.
Generally speaking, these routines are of use only to applications, such as
network management programs, that are intended to read the entire database.
With each set, an application can connect to a database,
return a database entry, and disconnect. The pattern for the names of these
routines is as follows:
X is the name of the
database.
The following function calls perform database services:
These routines make, send, and interpret data
packets for use with the Internet Domain Name Service. The resolver consists
of the following socket functions:
The following macros and functions place long and short
integers in a buffer in the format expected by the Internet name server:
Address or network information may
need to be translated, and
character sets received through a socket may need to be converted.
When
an integer is copied from a network packet (unit of data) to a local machine
or from a local machine to a network packet, the byte order must be converted
between local machine byte order and network standard byte order. The following
socket functions take a value as an argument and return that value with the
bytes rearranged:
The
following functions translate between the ASCII character set generally used
for text on the Internet, and the OS/390 or CMS EBCDIC character set:
The SAS/C
Socket Library supports both integrated
and non-integrated sockets. Integrated sockets are available with USS and
provide a higher degree of UNIX compatiblity. Non-integrated sockets rely
on a run-time library implemented interface to TCP/IP software. The socket
implementation is specified by the following function:
Note:
The socket implementation can
also be specified at run time by setting the _SASC_SOCKIMP environment variable.
In order to create a socket, the application issues a
socket
call, specifying the domain and family
to which the socket belongs. The program that creates the socket can also
control factors such as length of timeout, type of data transmitted, and size
of the communication buffer. Finally, the program can set operating characteristics
of the socket such as whether an application can continue processing without
becoming blocked during a
recv
call.
The following socket functions create a socket or control
its operating
characteristics:
A socket
is created without associating it to any
address. A local address can be bound to the socket with the following socket
function:
When an application in the role of server has opened a
stream socket, it binds the socket to a local address and then waits for a
connection request from another application. To avoid confusion in message
delivery, the application can set up a queue for the incoming connection requests.
The following socket function prepares the server to await incoming connections:
Connecting a socket binds
it to a permanent destination. If an
application seeks to transmit data to a stream socket, it must first establish
a connection to that socket. A server program can also pass sockets between
two client programs. The following socket functions establish or pass connections
between applications:
After a server has established a socket,
it waits for a connection. The following socket function causes the application
to wait for a connect request:
The following socket function accepts the next connection
on a socket and also receives the first block of data:
After a
socket has been established, an application can transmit or receive data through
the socket. The following socket functions are used in the transmission or
receipt of data between sockets:
An application
closes a connection when it has no more data to
send. An application can also shut down transmissions in one or both directions.
The following functions close the connection or shut down
transmissions:
Copyright © 2001
by SAS Institute Inc., Cary, NC, USA. All rights reserved.