Sockets were first developed at the University of California, Berkeley, in 1982 in order to support interprocess communication on networks. The client/server model is used for interprocess communication through sockets. Not all operating systems support sockets. If your system is one of these, the socket examples shown here will not work.
Sockets are a software abstraction representing the endpoints between two communicating processes, a server and a client; in other words, sockets allow processes to talk to each other. The communicating processes can be on the same machine or on different machines. The server process creates the socket. The client process knows the socket by name. In order to ensure where the data is coming from or going to on a network, the socket uses IP and port addresses. A program using a socket opens the socket (similar to opening a file), and once the socket is opened, I/O operations can be performed for reading and writing information to the communicating processes. When a file is opened, a file descriptor is returned. When a socket is opened, a socket descriptor is returned to the server and one is returned to the client. The socket interface, however, is much more complex than working with files, since communication is often done across networks.
Remote login and file transfer programs are common utilities that communicate across a network through the use of sockets.
Every socket has a type of communication path to identify how the data will be transferred through the socket. The two most common types of sockets, SOCK_STREAM and SOCK_DGRAM, utilize the TCP and UDP protocols, respectively.
There are other, less common types of sockets, such as SOCK_SEQPACKET and SOCK_RAW, but we will not discuss those here.
The SOCK_STREAM type of stream socket provides a reliable, connection-oriented, sequenced, two-way service. It provides for error detection and flow control, and it removes duplicate segments of data. The underlying protocol is TCP.
The SOCK_DGRAM type of datagram socket provides a connectionless service. Packets sent may be received in any order or may not be received on the other end at all. There is no guarantee of delivery and packets may be duplicated. The underlying protocol is UDP.
When a socket is opened, a number of system calls are issued describing how the socket is to be used. The process must specify a communication domain, also called an address family, to identify the way the socket is named and what protocols and addresses are needed in order to send or receive data. The domain of a socket identifies where the server and client reside—on the same machine, on the Internet, or on a Xerox network.
The two standard domains are the UNIX domain and the Internet domain. When using sockets for interprocess communication, the UNIX domain is used for processes communicating on the same machine, whereas the Internet domain is used for processes communicating on remote machines using the TCP/IP protocols. (The AF_NS domain is used for processes on a Xerox network.)
If the communication is between two processes on a local machine, the address family (AF) is called AF_UNIX. The UNIX domain supports the SOCK_STREAM socket type and the SOCK_DGRAM socket type. The SOCK_STREAM type in the UNIX domain provides a bidirectional communication between processes, similar to the pipe facility. SOCK_STREAM is a reliable byte-stream transfer between processes. The datagram sockets are unreliable, not often used, and cause the socket to behave like message queues or telegrams.
Sockets in the UNIX domain have pathnames in the UNIX file system. The ls -l command lists the file as a socket if the first character is an s (see Example 20.10). In the system file /usr/include/sys/socket.h, the AF_UNIX family is assigned the constant value 1.
$ ls -lF greetings srwxrwxrwx 1 ellie 0 Apr 26 09:31 greetings= |
The Internet domain sockets, identified as AF_INET, are used for interprocess communication between processes on different computers. The Internet domain also supports the socket types SOCK_STREAM and SOCK_DGRAM.
The Internet socket is defined by its IP address to identify the Internet host and its port number to identify the port on the host machine.
The underlying protocol for SOCK_STREAM is TCP. The underlying protocol for SOCK_DGRAM is UDP. In the system file /usr/include/sys/socket.h, the AF_INET family is defined as the constant 2.
A socket, once created, needs an address so that data can be sent to it. In the AF_UNIX domain, the address is a filename, but in the AF_INET domain, it is an IP address and a port number.
The socket function creates the socket and returns a filehandle for the socket. In the server this socket is called the rendezvous socket.
Formatsocket(SOCKET_FILEHANDLE, DOMAIN, TYPE, PROTOCOL); Example 20.11.
|
The bind function attaches an address or a name to a SOCKET filehandle. (See Example programs at the end of this chapter.)
Formatbind(SOCKET_FILEHANDLE, NAME); Example 20.12.
|
The listen function waits for accepts on the socket and specifies the number of connection requests to be queued before rejecting further requests. Imagine having call waiting on your phone with up to five callers queued.[3] If the number of client requests exceeds the queue size, an error is returned. The function returns true if successful, false otherwise. The error code is stored as $!.
[3] Wall, L., and Schwartz, R. L., Programming Perl, 2nd ed., O'Reilly & Associates: Sebastopol, CA, 1998, p. 158.
The accept function in the server process waits for a request from the client to arrive. If there is a queue of requests, the first request is removed from the queue when a connection is made. The accept function then opens a new socket filehandle with the same attributes as the original, or generic, socket, also called the rendezvous socket, and attaches it to the client's socket. The new socket is ready to communicate with the client socket. The generic socket is now available to accept additional connections. The accept function returns true if successful, false otherwise. The error code is stored as $!.
Formataccept(NEWSOCKET, GENERICSOCKET); Example 20.14.
|
The connect function uses the socket filehandle and the address of the server socket to which it will connect. It makes a rendezvous with the accept function in the server. After the connection is made, data can be transferred. If the UNIX domain is used, the pathname of the UNIX file is provided. If the Internet domain is used, the address is a packed network address of the proper type for the server. It returns true if successful, false otherwise. An error code is stored as $!.
Formatconnect(SOCKET, ADDRESS); Example 20.15.
|
The shutdown function shuts down a socket connection as specified. If the HOW argument is 0, further receives on the socket will be refused. If the HOW argument is 1, further sends on the socket will be refused, and if the argument is 2, all sends and receives are stopped.
Formatshutdown(SOCKET, HOW); Example 20.16.
|