This section describes the actual library functions for opening and closing sockets. The same functions work for all namespaces and connection styles.
The primitive for creating a socket is the socket function, declared in sys/socket.h. int function>socket/function> (int namespace, int style, int protocol) This function creates a socket and specifies communication style style, which should be one of the socket styles listed in the section called “Communication Styles”. The namespace argument specifies the namespace; it must be PF_LOCAL (the section called “The Local Namespace”) or PF_INET (the section called “The Internet Namespace”). protocol designates the specific protocol (the section called “Socket Concepts”); zero is usually right for protocol.
The return value from socket is the file descriptor for the new socket, or -1 in case of error. The following errno error conditions are defined for this function:
The protocol or style is not supported by the namespace specified.
The process already has too many file descriptors open.
The system already has too many file descriptors open.
The process does not have the privilege to create a socket of the specified style or protocol.
The system ran out of internal buffer space.
The file descriptor returned by the socket function supports both read and write operations. However, like pipes, sockets do not support file positioning operations.
For examples of how to call the socket function, see the section called “Example of Local-Namespace Sockets”, or the section called “Internet Socket Example”.
When you have finished using a socket, you can simply close its file descriptor with close; see the section called “Opening and Closing Files”. If there is still data waiting to be transmitted over the connection, normally close tries to complete this transmission. You can control this behavior using the SO_LINGER socket option to specify a timeout period; see the section called “Socket Options”.
You can also shut down only reception or transmission on a connection by calling shutdown, which is declared in sys/socket.h.
int function>shutdown/function> (int socket, int how) The shutdown function shuts down the connection of socket socket. The argument how specifies what action to perform:
Stop receiving data for this socket. If further data arrives, reject it.
Stop trying to transmit data from this socket. Discard any data waiting to be sent. Stop looking for acknowledgement of data already sent; don't retransmit it if it is lost.
Stop both reception and transmission.
The return value is 0 on success and -1 on failure. The following errno error conditions are defined for this function:
socket is not a valid file descriptor.
socket is not a socket.
socket is not connected.
A socket pair consists of a pair of connected (but unnamed) sockets. It is very similar to a pipe and is used in much the same way. Socket pairs are created with the socketpair function, declared in sys/socket.h. A socket pair is much like a pipe; the main difference is that the socket pair is bidirectional, whereas the pipe has one input-only end and one output-only end (Chapter 16).
int function>socketpair/function> (int namespace, int style, int protocol, int filedes[2]) This function creates a socket pair, returning the file descriptors in filedes[0] and filedes[1]. The socket pair is a full-duplex communications channel, so that both reading and writing may be performed at either end.
The namespace, style and protocol arguments are interpreted as for the socket function. style should be one of the communication styles listed in the section called “Communication Styles”. The namespace argument specifies the namespace, which must be AF_LOCAL (the section called “The Local Namespace”); protocol specifies the communications protocol, but zero is the only meaningful value.
If style specifies a connectionless communication style, then the two sockets you get are not connected, strictly speaking, but each of them knows the other as the default destination address, so they can send packets to each other.
The socketpair function returns 0 on success and -1 on failure. The following errno error conditions are defined for this function:
The process has too many file descriptors open.
The specified namespace is not supported.
The specified protocol is not supported.
The specified protocol does not support the creation of socket pairs.