You are here

TutorialLibraryTest ApplicationDownload
[Tutorial]LibraryTest ApplicationDownload

Socket Tutorial

Efficient multi-clients socket server in C/C++

Whatever the programming language, coding a client-server application is an essential step for any accomplished programmer. However, in the case of languages like C or C++, this this step can be laborious and can generate a large number of questions.

The reason for this is that it is almost mandatory (unless you use a more advanced library) to use low-level socket calls to the operating system. These are more or less standardized, in the sense that they are virtually identical regardless of the system (Windows, Linux, etc..). They are powerful because they can create any type of sockets based on either protocol or different types of addresses.

However, these features may seem obscure, especially for a beginner. They always require to delve carefully into their documentation. Moreover, the order in which socket calls must be proceeded is not really intuitive and often requires the use of examples.

This tutorial proposes to go into the details of the mechanisms used to create a socket management library. The library proposed here handles sockets based on TCP/IP protocol, which are used 99% of the time in practice. All sources are provided (see download section) and the code can be compiled under both Windows (tested with Visual Studio) and Linux .

In this article, I assume that the reader has a basic knowledge of C/C++, as well as of the TCP/IP protocol. Otherwise, I suggest to first document on the subject so you can follow this tutorial without difficulty. A basic understanding of the concept of thread is also preferable if the reader wishes to understand all the mechanisms of the library.

Finally, this tutorial is not exhaustive and does not exempt the reader to consider the documentation of the functions implementing socket mechanisms for its operating system: