Introducing TCP/IP concepts: Selecting sockets

You can choose among the following types of sockets:

  • Stream
  • Datagram
  • Raw

Stream sockets perform like streams of information. There are no record lengths or character boundaries between data, so communicating processes must agree on their own mechanisms for distinguishing information. Usually, the process sending information sends the length of the data, followed by the data itself. The process receiving information reads the length and then loops, accepting data until all of it has been transferred. Because there are no boundaries in the data, multiple concurrent read or write socket calls of the same type, on the same stream socket, will yield unpredictable results. For example, if two concurrent read socket calls are issued on the same stream socket, there is no guarantee of the order or amount of data that each instance will receive. Stream sockets guarantee to deliver data in the order sent and without duplication. The stream socket defines a reliable connection service. Data is sent without error or duplication and is received in the order sent. Flow control is built in to avoid data overruns. No boundaries are imposed on the data; the data is treated as a stream of bytes.

Stream sockets are most common because the burden of transferring the data reliably is handled by TCP/IP, rather than by the application.

The datagram socket is a connectionless service. Datagrams are sent as independent packets. The service provides no guarantees. Data can be lost or duplicated, and datagrams can arrive out of order. The size of a datagram is limited to the size able to be sent in a single transaction. Currently, the default value is 8192 bytes, and the maximum value is 65535. The maximum size of a datagram is 65535 for UDP and 65535 bytes for raw.

The raw socket allows direct access to lower layer protocols, such as IP and the ICMP. This interface is often used to test new protocol implementation, because the socket interface can be extended and new socket types defined to provide additional services. For example, the transaction type sockets can be defined for interfacing to the Versatile Message Transfer Protocol (VMTP). 1Transaction-type sockets are not supported by TCP/IP. Because socket interfaces isolate you from the communication function of the different protocol layers, the interfaces are largely independent of the underlying network. In the MVS™ implementation of sockets, stream sockets interface with TCP, datagram sockets interface with UDP, and raw sockets interface with ICMP and IP.
Notes:
  1. The TCP and UDP protocols cannot be used with raw sockets.
  2. If you are communicating with an existing application, you must use the same protocols used by the existing application. For example, if you communicate with an application that uses TCP, you must use stream sockets.
You should consider the following factors for these applications:
  • Reliability

    Stream sockets provide the most reliable connection. Datagrams and raw sockets are unreliable because packets can be discarded or duplicated during transmission. This characteristic might be acceptable if the application does not require reliability or if the application implements reliability beyond the socket interface.

  • Performance

    Overhead associated with reliability, flow control, and connection maintenance degrades the performance of stream sockets so that they do not perform as well as datagram sockets.

  • Data Transfer

    Datagram sockets limit the amount of data moved in a single transaction. If you send fewer than 2048 bytes of data at one time, use datagram sockets. When the amount of data in a single transaction is greater, use stream sockets.

If you are writing a new protocol to use on top of IP, or if you want to use the ICMP protocol, you must choose raw sockets; but to use raw sockets, you must be authorized by way of RACF® or APF.

1 David R. Cheriton and Carey L. Williamson, “MVSTP as the Transport Layer for High-Performance Distributed Systems,” IEEE Communications, June 1989, Vol. 27, No. 6.