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 for data exchange. While 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() call. In the internet domain, if a particular local address is needed, a bind() call must precede the first data transmission. Otherwise, the operating system sets the local address or port when data is first sent. The application program uses the sendto() and recvfrom() calls 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() call 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() call changes the destination address.

A connect() call 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() calls are not used with datagram sockets.

While a datagram socket is connected, errors from recent send() calls 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() call, can be used to interrogate the error status. A select() call 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.

See Socket Types for more information that you may need before connecting sockets.


[Back: Handling Multiple Sockets]
[Next: Socket Options]