Connectionless Datagram Services
The operating system provides support for connectionless interactions typical of the datagram facilities found in packet-switched networks. A datagram socket provides a symmetric interface to data exchange.
Although processes are still likely to be client and server, there is no requirement for connection establishment. Instead, each message includes the destination address.
An application program can create datagram sockets using the socket subroutine. In the Internet domain, if a particular local address is needed, a bind subroutine must precede the first data transmission. Otherwise, the operating system sets the local address or port when data is first sent. In the NDD domain, bind must precede the first data transmission. The application program uses the sendto and recvfrom subroutines to transmit data; these calls include parameters that allow the client process to specify the address of the intended recipient of the data.
In addition to the sendto and recvfrom calls, datagram sockets can also use the connect subroutine to associate a socket with a specific destination address. In this case, any data sent on the socket is automatically addressed to the connected peer socket, and only data received from that peer is delivered to the client process. Only one connected address is permitted for each socket at one time; a second connect subroutine changes the destination address.
A connect subroutine request on a datagram socket results in the operating system recording the peer socket's address (as compared to a stream socket, where a connect request initiates establishment of an end-to-end connection). The accept and listen subroutines are not used with datagram sockets.
While a datagram socket is connected, errors from recent send subroutines can be returned asynchronously. These errors can be reported on subsequent operations on the socket, or a special socket option, SO_ERROR. This option, when used with the getsockopt subroutine, can be used to interrogate the error status. A select subroutine for reading or writing returns true when a process receives an error indication. The next operation returns the error, and the error status is cleared.
Read the following concepts for more information that you might need before connecting sockets: