[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Communications Programming Concepts

Socket Types and Protocols

Socket subroutines take socket types and socket protocols as parameters. An application program specifying a socket type indicates the desired communication style for that socket or socket pair. An application program specifying a socket protocol indicates the desired type of service. This service must be within the allowable services of the protocol family.

Socket Types

Sockets are classified according to communication properties. Processes usually communicate between sockets of the same type. However, if the underlying communication protocols support the communication, sockets of different types can communicate.

Each socket has an associated type, which describes the semantics of communications using that socket. The socket type determines the socket communication properties such as reliability, ordering, and prevention of duplication of messages. The basic set of socket types is defined in the sys/socket.h file:

/*Standard socket types */
#define  SOCK_STREAM             1 /*virtual circuit*/
#define  SOCK_DGRAM              2 /*datagram*/
#define  SOCK_RAW                3 /*raw socket*/
#define  SOCK_RDM                4 /*reliably-delivered message*/
#define  SOCK_CONN_DGRAM         5 /*connection datagram*/

Other socket types can be defined.

The operating system supports the following basic set of sockets:

SOCK_DGRAM Provides datagrams, which are connectionless messages of a fixed maximum length. This type of socket is generally used for short messages, such as a name server or time server, because the order and reliability of message delivery is not guaranteed.

In the UNIX domain, the SOCK_DGRAM socket type is similar to a message queue. In the Internet domain, the SOCK_DGRAM socket type is implemented on the User Datagram Protocol/Internet Protocol (UDP/IP) protocol.

A datagram socket supports the bidirectional flow of data, which is not sequenced, reliable, or unduplicated. A process receiving messages on a datagram socket may find messages duplicated or in an order different than the order sent. Record boundaries in data, however, are preserved. Datagram sockets closely model the facilities found in many contemporary packet-switched networks.

SOCK_STREAM Provides sequenced, two-way byte streams with a transmission mechanism for stream data. This socket type transmits data on a reliable basis, in order, and with out-of-band capabilities.

In the UNIX domain, the SOCK_STREAM socket type works like a pipe. In the Internet domain, the SOCK_STREAM socket type is implemented on the Transmission Control Protocol/Internet Protocol (TCP/IP) protocol.

A stream socket provides for the bidirectional, reliable, sequenced, and unduplicated flow of data without record boundaries. Aside from the bidirectionality of data flow, a pair of connected stream sockets provides an interface nearly identical to pipes.

SOCK_RAW Provides access to internal network protocols and interfaces. Available only to individuals with root-user authority, a raw socket allows an application direct access to lower-level communication protocols. Raw sockets are intended for advanced users who wish to take advantage of some protocol feature that is not directly accessible through a normal interface, or who wish to build new protocols atop existing low-level protocols.

Raw sockets are normally datagram-oriented, though their exact characteristics are dependent on the interface provided by the protocol.

SOCK_SEQPACKET Provides sequenced, reliable, and unduplicated flow of information.
SOCK_CONN_DGRAM Provides connection-oriented datagram service. This type of socket supports the bidirectional flow of data, which is sequenced and unduplicated, but is not reliable. Because this is a connection-oriented service, the socket must be connected prior to data transfer. Currently, only the Asynchronous Transfer Mode (ATM) protocol in the Network Device Driver (NDD) domain supports this socket type.

The SOCK_DGRAM and SOCK_RAW socket types allow an application program to send datagrams to correspondents named in send subroutines. Application programs can receive datagrams through sockets using the recv subroutines. The Protocol parameter is important when using the SOCK_RAW socket type to communicate with low-level protocols or hardware interfaces. The application program must specify the address family in which the communication takes place.

The SOCK_STREAM socket types are full-duplex byte streams. A stream socket must be connected before any data can be sent or received on it. When using a stream socket for data transfer, an application program needs to perform the following sequence:

  1. Create a connection to another socket with the connect subroutine.
  2. Use the read and write subroutines or the send and recv subroutines to transfer data.
  3. Use the close subroutine to finish the session.

An application program can use the send and recv subroutines to manage out-of-band data.

SOCK_STREAM communication protocols are designed to prevent the loss or duplication of data. If a piece of data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable period of time, the connection is broken. When this occurs, the socket subroutine indicates an error with a return value of -1 and the errno global variable is set to ETIMEDOUT. If a process sends on a broken stream, a SIGPIPE signal is raised. Processes that cannot handle the signal terminate. When out-of-band data arrives on a socket, a SIGURG signal is sent to the process group.

The process group associated with a socket can be read or set by either the SIOCGPGRP or SIOCSPGRP ioctl operation. To receive a signal on any data, use both the SIOCSPGRP and FIOASYNC ioctl operations. These operations are defined in the sys/ioctl.h file.

Socket Protocols

A protocol is a standard set of rules for transferring data, such as UDP/IP and TCP/IP. An application program can specify a protocol only if more than one protocol is supported for this particular socket type in this domain.

Each socket can have a specific protocol associated with it. This protocol is used within the domain to provide the semantics required by the socket type. Not all socket types are supported by each domain; support depends on the existence and implementation of a suitable protocol within the domain.

The /usr/include/sys/socket.h file contains a list of socket protocol families. The following list provides examples of protocol families (PF) found in the socket header file:

PF_UNIX Local communication
PF_INET Internet (TCP/IP)
PF_NS Xerox Network System (XNS) architecture
PF_NDD The operating system NDD

These protocols are defined to be the same as their corresponding address families in the socket header file. Before specifying a protocol family, the programmer should check the socket header file for currently supported protocol families. Each protocol family consists of a set of protocols. Major protocols in the suite of Internet Network Protocols include:

Read more about these protocols in "Internet Transport-Level Protocols" in AIX 5L Version 5.2 System Management Guide: Communications and Networks.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]