Sockets


As described earlier, all messages sent through connections are transmitted using sockets. When you use connections you do not have to fully understand the nuances of sockets, but a general understanding of sockets helps to illustrate the advantages of using connections instead of just sockets. This section gives a brief tutorial on sockets.

Protocols: TCP/IP and Local

A computer network can be viewed as having several layers:

  1. Highest Layer: User Applications (RTserver, connection programs, and so on)
  1. Middle Layers: Software Protocols (TCP/IP, and so on)
  2. Lowest Layer: Hardware Protocols (Ethernet, Token Ring, and so on)

Most computers support Ethernet, some Token ring, and almost all support the widely available Transmission Control Protocol/Internet Protocol (TCP/IP). TCP/IP was developed at the University of California at Berkeley for BSD UNIX. There are several commercial TCP/IP packages available for OpenVMS, such as MultiNet. To support heterogeneous networks using Ethernet or Token Ring, such as Solaris, Windows, or OpenVMS, SmartSockets supports TCP/IP. In fact, within a single application, a process can communicate with different processes using different protocols.

In addition to the above IPC protocols that send data over a network, it is often useful to perform IPC between two processes on the same computer. This can be thought of as a local IPC protocol. SmartSockets IPC also supports this local protocol.

What is a Socket?

A socket is a software interface to an IPC protocol. Each socket uses one specific IPC protocol. A good analogy for the socket is the telephone. A socket allows one process to speak to another process, much like a telephone allows one person to talk to another. There are rules for how the two processes must make the connection (similar to how one must pick up a telephone, dial a number, and have someone answer at the other end).

There are several different types of sockets. The most common type is called a stream socket, and it is the type used by SmartSockets IPC. Other kinds of sockets include datagram sockets and raw sockets. From this point on, the term socket is used to mean a stream socket. Unlike a telephone, sockets are truly bidirectional: both processes can write (talk) and read (listen) at the same time.

Most current operating systems support sockets. Sockets are very portable, though there are differences between the various implementations of sockets. None of the OpenVMS implementations of sockets, however, support the TCP/IP and local IPC protocols that SmartSockets can use. On OpenVMS, SmartSockets supplies its own socket functions (which are implemented using native OpenVMS system calls like SYS$QIO) to allow TCP/IP and local sockets to be used by the same program. See the TIBCO SmartSockets Utilities reference for more information on how to use these socket functions.

How Sockets Work

Each socket has four buffers (one for each process for each direction) associated with it. These buffers are usually at least 8192 bytes each, but the size of each buffer can be changed dynamically. Figure 3 shows these buffers.

Figure 3 Socket Data Buffering

When one process writes data to a socket, the data is copied into the appropriate outgoing buffer, and then the operating system takes care of transmitting the data to the incoming buffer for the receiving process. If the incoming buffer fills up or if there is a hardware or software failure, then the outgoing buffer starts to fill up. If the outgoing buffer is full, the process either waits for space in the buffer or gets an error condition. When the process at the other end of the socket reads data from the socket, the data comes out of the socket’s incoming buffer. If the incoming buffer is empty, the process either waits for data to appear or gets an error condition. When a processes waits, it blocks (does not use CPU time) until the socket is ready again.

Sockets handle several types of simple network failures, such as reordering data that arrives out of order and resending data that doesn’t arrive. They do not take care of recovery from more fatal types of errors, however, such as processes or nodes crashing. See Chapter 4, Guaranteed Message Delivery for details on how connections overcome these more serious failures.

Sockets are first in, first out (FIFO) byte streams, just like UNIX files. A byte stream does not distinguish between message boundaries. If one process writes 64 bytes to a socket, a process at the other end could decide to read 32 bytes twice, or 8 bytes 8 times, or even 1 byte 64 times. It is more efficient, however, for the reading process to read 64 bytes one time. Sockets are fairly fast on Ethernet, where data transfer rates of 500 kilobytes/second and up are possible.

Because sockets are byte streams, it is up to the two communicating processes to agree on what the bytes mean: some sort of protocol is necessary. If the two processes are on computers that don't use the same formats for integers, floating-point numbers, or character strings, then it becomes more difficult to pass binary data between the processes.

Sockets have several advantages over other UNIX IPC mechanisms. STREAMS and TLI also provide an interface to IPC protocols, but are not as portable as sockets. Pipes are similar to sockets, but they are unidirectional (data can only be transferred one way) and can only be used between related processes (usually where one process is a child of the other process). They cannot be used between unrelated processes. Shared memory allows unrelated processes on the same machine to communicate, but it is somewhat clumsier to use and does not provide a byte stream. Neither shared memory nor pipes allow processes on different computers to communicate.

Advantages of Connections Over Sockets

Connections are implemented using sockets and take advantage of all the features that sockets have to offer. Connections also have many features that make them easier to use and more powerful than sockets:


TIBCO SmartSockets™ User’s Guide
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com