Previous Page Next Page

20.5. Sockets

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.

20.5.1. Types 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.

Stream Sockets

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.

Datagram Sockets

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.

20.5.2. Socket Domains

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.)

The UNIX Domain and the AF_UNIX Family

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.

Example 20.10.

$ ls -lF greetings
srwxrwxrwx             1  ellie     0 Apr 26 09:31   greetings=

The Internet Domain and the AF_INET Family

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.

Socket Addresses

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.

20.5.3. Creating a Socket

The socket function creates the socket and returns a filehandle for the socket. In the server this socket is called the rendezvous socket.

Format

socket(SOCKET_FILEHANDLE, DOMAIN, TYPE, PROTOCOL);

Example 20.11.

   $AF_UNIX=1;
   $SOCK_STREAM=1;
   $PROTOCOL=0;

1  socket(COMM_SOCKET, $AF_UNIX, $SOCK_STREAM, $PROTOCOL);

Explanation

  1. In this example, the socket function creates a filehandle called COMM_SOCKET. The domain or family is UNIX, the socket type is STREAM, and the protocol is assigned 0, which allows the system to choose the correct protocol for this socket type.


20.5.4. Binding an Address to a Socket Name

The bind Function

The bind function attaches an address or a name to a SOCKET filehandle. (See Example programs at the end of this chapter.)

Format

bind(SOCKET_FILEHANDLE, NAME);

Example 20.12.

1  bind(COMM_SOCKET, "/home/jody/ellie/perl/tserv");
2  bind(COMM_SOCKET, $socket_address);

Explanation

  1. When using the UNIX domain, the socket filehandle COMM_SOCKET is bound to a UNIX pathname.

  2. When using the Internet domain, the socket filehandle COMM_SOCKET is bound to the packed address for the socket type.


20.5.5. Creating a Socket Queue

The listen Function

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.

Format

listen(SOCKET_FILEHANDLE, QUEUE_SIZE);

Example 20.13.

listen(SERVERSOCKET, 5);

Explanation

The number of waiting connections in the queue is set to 5. The listen function queues the incoming client requests for the accept function (see following).


20.5.6. Waiting for a Client Request

The accept Function

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 $!.

Format

accept(NEWSOCKET, GENERICSOCKET);

Example 20.14.

accept (NEWSOCK, RENDEZ_SOCK );

Explanation

The server process uses the accept function to accept requests from clients. It takes the first pending request from the queue and creates a new socket filehandle, NEWSOCK, with the same properties as RENDEZ_SOCK filehandle, also called the generic or rendezvous socket.


20.5.7. Establishing a Socket Connection

The connect Function

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 $!.

Format

connect(SOCKET, ADDRESS);

Example 20.15.

1  connect(CLIENT-SOCKET, "/home/joe/sock" );
2  connect(CLIENTSOCKET, $packed_address);

Explanation

  1. In the UNIX domain, a UNIX pathname is attached to the filehandle CLIENTSOCKET.

  2. In the Internet domain, a packed network address is attached to the filehandle CLIENTSOCKET. The address is obtained from the gethostbyname function and is packed with the Perl pack function. For example:

         $hostname="houston";
         $port=9876;
         $AF_INET=2;
         $SOCK_STREAM=1;
        ($name, $aliases, $type, $len, $address)=gethostbyname($hostname);
         $packed_address=pack(S n a4 x8, $AF_INET, $port, $address);


20.5.8. Socket Shutdown

The shutdown Function

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.

Format

shutdown(SOCKET, HOW);

Example 20.16.

shutdown(COMM_SOCK, 2);

Explanation

The socket filehandle COMM_SOCK will disallow further sends or receives. It is shut down.


Figure 20.1. Sockets for a server and client connection-oriented communication.


Previous Page Next Page