Communication via Sockets

From the programmer's view, external devices are simply regular files under Linux (and Unix) that are accessed by normal read and write operations, as described in Chapter 8. This simplifies access to resources because only a single, universal interface is needed.

The situation is a bit more complicated with network cards because the above scheme either cannot be adopted at all or only with great difficulty. Network cards function in a totally different way from normal block and character devices so that the typical Unix motto that ''everything is a file'' no longer fully applies.3 One reason is that many different communication protocols are used (in all layers) where many options need to be specified in order to establish a connection — and this cannot be done when device files are opened. Consequently, there are no entries for network cards in the /dev directory.4

Of course, the kernel must provide an interface that is as universal as possible to allow access to its network functions. This problem is not Linux-specific and gave BSD Unix programmers headaches in the 1980s. The solution they adopted — special structures called sockets that are used as an interface for network implementation — has now established itself as an industry standard. Sockets are defined in the POSIX standard and are therefore also implemented by Linux.

Sockets are now used to define and set up network connections so that they can be accessed (particularly by read and write operations) using the normal means of an inode. In the view of programmers, the ultimate result of socket creation is a file descriptor that provides not only the whole range of standard functions but also several enhanced functions. The interface used for the actual exchange of data is the same for all protocols and address families.

When a socket is created, a distinction is made not only between address and protocol families but also between stream-based and datagram-based communication. What is also important (with stream-oriented sockets) is whether a socket is generated for a client or for a server program.

To illustrate the function of a socket from a user point of view, I include a short sample program to demonstrate just a few of the network programming options. Detailed descriptions are provided in numerous specialized publications, [Ste00], for example.

Continue reading here: Creating a Socket

Was this article helpful?

0 0