| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This repository contains pure C code for working with sockets on a POSIX operating system. There is code for both UNIX and Internet domain sockets using both stream and datagram sockets. For each configuration there is code for both a client and a server.
In general I would prefer to write code in C++ as compared to C due primarily to the vastly superior standard library, but by using pure C code, it can be used with either a C or C++ baseline.
Further details are available within the subdirectory for each example in the README.md there.
This code is all based on code from the truly excellent book The Linux Programming
Interface (TLPI for short) by Michael Kerrisk. If you haven't had the pleasure of being exposed to this book it is one of the best reference works I have ever seen. Check it out:

I have renamed, refactored, and reformatted in various ways to arrive at code with a coding style which I prefer. I have also added some new comments and expanded upon existing ones to make the code as self-documenting as possible.
But Mr. Kerrisk deserves kudos for writing very readable code with proper error handling which is as simple as it can be while getting the job done.
Building this code requires the following:
cd into the directory for the example you wish to build and then do the following
mkdir build cd build cmake .. make
See instructions within the README.md for the example you wish to run.
Depending on your needs, you may be a lot better off using a higher-level library which abstracts away some of the intricacies (pains) of dealing with sockets. In particular, 0MQ / ZeroMQ is an excellent library which has many advantages over direct socket programming:
Stream sockets are wonderful in that they are reliable. What this means is that we are guaranteed that either the transmitted data will arrive intact at the receiving application, exactly as it was transmitted by the sender (assuming that neither the network link nor the receiver crashes), or that we will receive notification of a probable failure in transmission.
But there are a couple caveats that you need to be aware of otherwise they will bite you very hard:
Datagram sockets have the advantages of preserving message boundaries and being connectionless. This means you don't need to deal with message fragmentation, which is rather swell. However, the down side with datagrams is that they are unreliable and you will typically need to implement an application-level protocol for dealing with this unreliability, which can get quite complicated in it's own right.
You need to be aware of the following possibilities and generally deal with them:
NOTE: UNIX domain datagram sockets are actually fully reliable, so you don't need to worry about the points mentioned above with them.
| Back | FazBrowse Home | New Git URL |