Communication through sockets

Sockets are widely used in communication between computer programs (processes) on different computers and even on programs on the same computer. Essentially, sockets are bi-directional full-duplex communication plugs. To be able to communicate, sockets operate in pairs. In-between a pair of sockets a connection can be set up or sockets send each other messages, depending on the type of sockets used.

There are several types of sockets. The most important classification is into datagram- and stream sockets.

Datagram sockets are connectionless. These sockets communicate using datagrams: small packages of data. There is no permanent connection between these sockets. Every write action sends a message which must be read by the receiver in one read action. Thus the boundaries between messages is visible to the sender and receiver and synchronisation of the messages is left to the sending and receiving program.
If you're using datagrams sockets you have to be aware that delivery of packages of data is not guaranteed. It is even possible to receive the same package more than once.

Stream sockets are connection-oriented, meaning that they will build up a connection over which data traffic can take place. Writing data to this type of sockets and reading data from them is like writing to or reading from a byte stream. There are no package boundaries and byte order is preserved by the transport system.
With stream sockets, correct delivery of data is guaranteed.

In the internet domain (AF_INET) the default protocol for stream sockets is TCP (transport control protocol) and for datagram sockets it is UDP (user datagram protocol). Hence we have tcp/ip and udp/ip.

Applications of sockets:
The following examples demonstrate several aspects of working with sockets. The Makefile can be used to build all examples. The examples have been developed and tested on a PC running Debian Sarge and on a Mac running OS-X.


Assignment: equip the daemon with a message queue

Socket stream