Transport Interfaces Programming Guide

What Are Sockets?

Sockets are the Berkeley UNIX interface to network protocols. They have been an integral part of SunOS releases since 1981. They are commonly referred to as Berkeley sockets or BSD sockets. Beginning in Solaris 7, the XNS 5 (Unix98) Socket interfaces (which differ slightly from the BSD sockets) are also available.

The XNS 5 (Unix98) Socket interfaces are documented in the following man pages: accept(3XN), bind(3XN), connect(3XN), endhostent(3XN), endnetent(3XN), endprotoent(3XN), endservent(3XN), gethostbyaddr(3XN), gethostbyname(3XN), gethostent(3XN), gethostname(3XN), getnetbyaddr(3XN), getnetbyname(3XN), getnetent(3XN), getpeername(3XN), getprotobyname(3XN), getprotobynumber(3XN), getprotoent(3XN), getservbyname(3XN), getservbyport(3XN), getservent(3XN), getsockname(3XN), getsockopt(3XN), htonl(3XN), htons(3XN), inet_addr(3XN), inet_lnaof(3XN), inet_makeaddr(3XN), inet_netof(3XN), inet_network(3XN), inet_ntoa(3XN), listen(3XN), ntohl(3XN), ntohs(3XN), recv(3XN), recvfrom(3XN), recvmsg(3XN), send(3XN), sendmsg(3XN), sendto(3XN), sethostent(3XN), setnetent(3XN), setprotoent(3XN), setservent(3XN), setsockopt(3XN), shutdown(3XN), socket(3XN), and socketpair(3XN). The traditional SunOS 5 BSD Socket behaviour is documented in the corresponding 3N man pages. See the standards(5) man page for information on building applications that use the XNS 5 (Unix98) socket interface.

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:

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.

Socket Libraries

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.


Note -

Static linking is strongly discouraged.


Socket Types

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:

  1. 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 SOCK_STREAM.

  2. 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 SOCK_DGRAM.

  3. 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 SOCK_RAW.

See "Selecting Specific Protocols" for further information.