|
|
This section describes the sockets programming interface. The special needs of network communication are discussed in general terms, and socket types and addressing schemes are introduced. After a brief examination of elementary socket system calls, a few suggestions for data transfer and byte swapping for manipulation of names and addresses are offered. The focus of the discussion is primarily on connection-oriented streams sockets, although some reference is made to connectionless datagram sockets. The section concludes with annotated examples of client and server socket programs. Signals and error codes are not discussed.
The Sockets Library (SLIB) provides an application program interface (API) for communication between processes. For the Internet domain, the processes can communicate across a network protocol; for the UNIX domain, the processes must be executing on the same system. Sockets are bidirectional, providing a two-way flow of data between processes that may or may not have the same parent.
Pipes provide a similar functionality, but are unidirectional and can be used only between processes that have the same parent; this implies that the two processes must be executing on the same host. To obtain a bidirectional flow of data with pipes, the pipe(S) system call must be executed twice, returning four file descriptors. Communication between processes in a distributed environment should not depend on whether the processes are on the same machine. Unlike pipes, a connected socket returns a pair of socket descriptors, not file descriptors. Socket descriptors are assigned by the kernel with the accept(SSC) and socket(SSC) system calls.
Network I/O and file
I/O differ in significant ways.
The UNIX system uses six system calls to perform
file I/O:
open(S),
creat(S),
close(S),
read(S),
write(S),
and
lseek(S).
These system calls work with file descriptors.
Network I/O, however, is more complex and involves more options
than the file descriptor semantics of the UNIX filesystem.
Consider the following: