This chapter presents the socket interface and illustrates it with sample programs. This chapter discusses:
"SunOS 4 Binary Compatibility" discusses binary compatibility with the SunOS 4 environment
Socket creation, connection, and closure are discussed in "Socket Basics"
Client-Server architecture is discussed in "Client-Server Programs"
Advanced topics, discussed in "Advanced Socket Topics", include multicast and asynchronous sockets
Note - The interface described in this chapter is multithread safe. You can call applications that contain socket interface calls freely in a multithreaded application. Note, however, that the degree of concurrency available to applications is not specified.
SunOS 4 Binary Compatibility
Two major changes from the SunOS 4 environment hold true for SunOS 5.9 releases. The binary compatibility package enables dynamically linked socket applications based on SunOS 4to run on SunOS 5.9.
You must explicitly specify the socket library (-lsocket or libsocket) on the compilation line.
You might also need to link with libnsl using -lsocket -lnsl, not -lnsl -lsocket.
You must recompile all SunOS 4 socket-based applications with the socket library to run in a SunOS 5.9 environment.
Overview of Sockets
Sockets have been an integral part of SunOS releases since 1981. 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 the UNIX family.
The interface to network protocols needs to accommodate server code that waits for connections and client code that initiates connections.
Operations differ depending on whether communication is connection-oriented or connectionless.
Application programs might want to specify the destination address of the datagrams they are delivering instead of binding the address with the open(2) call.
Sockets make network protocols available while behaving like UNIX files. Applications create sockets when they are needed. Sockets work with the close(2), read(2), write(2), ioctl(2), and fcntl(2) interfaces. The operating system differentiates between the file descriptors for files and the file descriptors for sockets.
The socket interface routines are in a library that must be linked with the application. The library libsocket.so is contained in /usr/lib with the rest of the system service libraries. Use libsocket.so for dynamic linking.
Socket types define the communication properties visible to a user. The Internet family sockets provide access to the TCP/IP transport protocols. The Internet family is identified by the value AF_INET6, for sockets that can communicate over both IPv6 and IPv4. The value AF_INET is also supported for source compatibility with old applications and for "raw" access to IPv4.
The SunOS environment supports three types of sockets:
Stream sockets enable processes to communicate using TCP. A stream socket provides a 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.
Datagram sockets enable processes to use UDP to communicate. A datagram socket supports a 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.
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.
The SunOS 5.9 platform provides two sets of socket interfaces. The BSD socket interfaces are provided and, since SunOS version 5.7, the XNS 5 (Unix98) socket interfaces are also provided. The XNS 5 interfaces differ slightly from the BSD interfaces.
The XNS 5 socket interfaces are documented in the following man pages: