Sockets are the 4.2 Berkeley software distribution (BSD) UNIX interface to network protocols. They havebeen an integral part of SunOS releases since 1981. They are commonly referred to as Berkeley sockets or BSD sockets. Since the days of early UNIX, applications have used the file system model of input/output to access devices and files. The file system model is sometimes called open-close-read-write after the basic function calls used in this model. However, the interaction between user processes and network protocols are more complex than the interaction between user processes and I/O devices.
A socket is an endpoint of communication to which a name can be bound. A socket has a type and one associated process. Sockets were designed to implement the client-server model for interprocess communication where:
The interface to network protocols needs to accommodate multiple communication protocols, such as TCP/IP, Xerox internet protocols (XNS), and UNIX domain.
The interface to network protocols needs to accommodate server code that waits for connections and client code that initiates connections.
It also needs to operate differently, depending on whether communication is connection-oriented or connectionless.
Application programs might want to specify the destination address of the datagrams it delivers instead of binding the address with the open() call.
To address these issues and others, sockets are designed to accommodate network protocols, while still behaving like UNIX files or devices whenever it makes sense. Applications create sockets when they are needed. Sockets work with the open(), close(), read(), and write() function calls, and the operating system can differentiate between the file descriptors for files, and file descriptors for sockets.
UNIX domain sockets are named with UNIX paths. For example, a socket might be named /tmp/foo. UNIX domain sockets communicate only between processes on a single host. Sockets in the UNIX domain are not considered part of the network protocols because they can only be used to communicate with processes within the same UNIX system. They are rarely used today and are only briefly covered in this manual.
The socket interface routines are in a library that must be linked with the application. The libraries libsocket.so and libsocket.a are contained in /usr/lib with the rest of the system service libraries. The difference is that libsocket.so is used for dynamic linking, whereas libsocket.a is used for static linking.
Socket types define the communication properties visible to a user. The Internet domain sockets provide access to the TCP/IP transport protocols. The Internet domain is identified by the value
AF_INET. Sockets exchange data only with sockets in the same domain.
Three types of sockets are supported:
Stream sockets allow processes to communicate using TCP. A stream socket provides bidirectional, reliable, sequenced, and unduplicated flow of data with no record boundaries. After the connection has been established, data can be read from and written to these sockets as a byte stream. The socket type is
Datagram sockets allow processes to use UDP to communicate. A datagram socket supports bidirectional flow of messages. A process on a datagram socket can receive messages in a different order from the sending sequence and can receive duplicate messages. Record boundaries in the data are preserved. The socket type is
Raw sockets provide access to ICMP. These sockets are normally datagram oriented, although their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not for most applications. They are provided to support developing new communication protocols or for access to more esoteric facilities of an existing protocol. Only superuser processes can use raw sockets. The socket type is
See "Selecting Specific Protocols" for further information.