The BSD UNIX Socket Library |
BSD
UNIX communications programming is based on the original UNIX framework, with
some additions and elaborations to take into account the greater complexity
of interprocess communications. In traditional UNIX file I/O, an application
issues an
open
call, which
returns a file descriptor (a small integer) bound to a particular file or
device. The application then issues a
read
or
write
call
that causes the transfer of stream data. At the end of communications, the
application issues a
close
call to terminate the interaction.
Because interprocess communication often takes place
over a network, the BSD UNIX Socket Library takes into account the numerous
variables of network I/O, such as network protocols, in addition to the semantics
of the UNIX file system.
Briefly stated, a socket is an end point
for interprocess communication, in this case, over a network running TCP/IP.
The socket interface can support a number of underlying transport mechanisms.
Ideally, a program written with socket calls can be used with different network
architectures and different local interprocess communication facilities with
few or no changes. The SAS/C Compiler supports TCP/IP and the AF_INET Internet
addressing family.
Sockets can simultaneously transmit and receive data
from another process, using semantics that depend on the type of socket. There
are three types of sockets: stream, datagram, and raw, each of which represents
a different type of communications service.
Stream sockets provide reliable, connection-based
communications. In connection-based communications, the two processes must
establish a logical connection with each other. A stream of bytes is then
sent without errors or duplication and is received in the order in which it
was sent. Stream sockets correspond to the TCP protocol in TCP/IP.
Datagram sockets communicate via discrete
messages, called datagrams, which are sent as packets. Datagram sockets are
connectionless; that is, the communicating processes do not have a logical
connection with each other. The delivery of their data is unreliable. The
datagrams can be lost or duplicated, or they may not arrive in the order in
which they were sent. Datagram sockets correspond to the UDP protocol in
TCP/IP.
Raw sockets provide direct access to the
lower-layer protocols, for example, IP and the Internet Control Message Protocol
(ICMP).
If you are communicating with an existing application,
you must use the same protocols as that application. When symbols defined
in the header files are passed as parameters to the socket functions, you
can change options and variables, such as whether your communications are
connection-oriented or connectionless and what network names you want to send
to or receive from.
In network operations, an application can specify a
different destination each time it uses a socket. It is also possible to
send datagrams of varying length, in addition to stream data. The socket interface
enables you to create both servers that await connections and clients that
initiate the connections using services in addition to
open
,
read
,
write
, and
close
.
Another factor that makes communicating over a network
more complex than traditional UNIX I/O is the problem of ascertaining the
correct addresses for clients and servers, and of converting information from
one format to another as it travels between different machines on different
networks in the Internet. The socket library provides Internet addressing
utility functions and network information functions to resolve these issues.
Copyright © 2001
by SAS Institute Inc., Cary, NC, USA. All rights reserved.