# Hands-On Network Programming with C: the example code on its own

> The repository behind the book Hands-On Network Programming with C is a directory per chapter of small, complete, portable programs, from Winsock initialisation through TCP and UDP servers, DNS encoding, HTTP, SMTP, TLS with OpenSSL, and SSH with libssh. Every program is written to compile cleanly as C or C++ on Windows, Linux, and macOS.

**codeplea/Hands-On-Network-Programming-with-C** — Book Example Code for Hands-On Network Programming with C

- Repository: https://github.com/codeplea/Hands-On-Network-Programming-with-C
- Website: https://handsonnetworkprogramming.com/
- Stars: 752 · Forks: 181
- Language: C
- License: MIT
- Published: 2026-10-21 · Updated: 2026-10-21 · Language: en
- Canonical page: https://hysenlabs.com/projects/codeplea-hands-on-network-programming-with-c

## A book with the code published alongside it

This is the code repository for the book Hands-On Network Programming with C, written by Lewis Van Winkle and published by Packt, and the code is released under the MIT licence. The book site is handsonnetworkprogramming.com, and the description of the book in the repository is a single line: learn socket programming in C and write secure and optimized network code.

The framing of the project matters for how you use it. This is not a library and it is not a framework, it is a set of example programs, and the README calls it the book example code in the repository description. What that gives you is a directory per chapter with small, complete, compilable programs, and a narrative order that goes from initialising the socket API to running a TLS client.

The book's stated features describe the ground the examples cover. You uncover cross-platform socket programming APIs, implement techniques for supporting IPv4 and IPv6, understand how TCP and UDP connections work over IP, discover how hostname resolution and DNS work, interface with web APIs using HTTP and HTTPS, get hands-on experience with the email protocol over SMTP, gain real-world expertise with practical network applications, and explore new approaches for the Internet of Things.

The repository is not archived and the last push was on 2026-08-13, and there are no releases, which is right for a set of files that are tagged against a book edition rather than versioned as a package.

The top level is almost entirely the chapter directories, from chap01 to chap11 and then chap13, plus a test directory and the continuous integration configuration. The absence of a chap12 directory is not an oversight, since the README says that chapter does not include any example programs.

## The portability promise, and how it is kept

The README makes one promise about the code, and it is the promise that decides whether this repository is useful to you.

Unless otherwise noted, all programs will compile cleanly on Windows, Linux, and macOS, and as C or as C++. The code is tested with MinGW and Visual Studio on Windows, and with gcc and clang on Linux and macOS.

That is a stronger claim than most sample code makes, and it is why the repository is worth reading even if you have the book. A socket program that only builds on Linux teaches you Linux, whereas a program that builds with two compilers on three systems shows you the parts of the API that are portable and, by implication, the parts that are not.

The first chapter shows the cost of that promise. It has a Windows only example for initialising Winsock and a Windows only example for listing local IP addresses, and a separate Unix and macOS example for the same listing task. The duplication is not sloppiness, it is the demonstration that the platform differences are real and that the book names them rather than hiding them behind a compatibility layer.

The same pattern runs through the rest of the book. In chapter three there is a TCP server that uses select and a second one that uses fork, and the fork based version is marked as Linux and macOS only. The non-blocking and timeout examples in the later chapters are listed as pairs, one of the pair for comparison with the other, which is a nice way of showing the difference between two approaches to the same problem.

## The first four chapters: sockets, TCP, and UDP

The early chapters build up the API in the order a reader needs it.

Chapter two is about setting up. There is an example program that includes all the needed headers and initialises, a program that prints the current date and time to the console, and then the same idea as a network service. The time server serves a web page giving the current date and time, and there is an IPv6 variant, and then a dual stack variant that listens for IPv6 and IPv4 connections together.

The three time server programs are the sharpest teaching device in the repository, because the only thing that changes between them is the socket setup, and the difference between an IPv6 only listener and a dual stack one is exactly the sort of thing that is hard to see in prose and obvious in code.

Chapter three is TCP. There is a client that establishes a connection and sends and receives data from the console. The server example services multiple connections using select and echoes what it receives back in upper case, with the fork based variant alongside it. The third server relays received data to every other connected client, which is a chat server in thirty lines, and the chapter is where the concurrency model of the whole book is set.

Chapter four is UDP, and the examples are built to show the API differences rather than the concepts. There is a client that sends and receives data from the console, a program that uses recvfrom to receive one packet, one that uses sendto to send one packet, a server that echoes data back to the sender in upper case, and a simpler version of that server that does not use select.

The select versus not-select pairing in the UDP chapter is the same pedagogical device as the fork pairing in the TCP chapter, and it makes the point that select is what you reach for when one process must serve many sockets.

## Resolution, DNS, and the first HTTP request

Chapter five is where names meet addresses, and it contains two programs with very different levels of abstraction.

The first uses getaddrinfo to look up the addresses for a given hostname, which is the portable way to resolve a name, and the second encodes and sends a DNS query and then decodes the response. The pair is deliberate: one shows you the API you should use, and the other shows you what is happening underneath it, which is the same pedagogical pattern as the select and fork pairs.

Chapter six is a single program, a minimal HTTP client that downloads a web resource from a given URL. Minimal is the important word, and it is the word that describes most of the programs in this repository. There is no URL parser, no redirect handling, no keep-alive, and no TLS, because the point of the chapter is to show the shape of a request and a response on the wire.

Chapter seven turns it around with two minimal web servers, and the second of them is described as a minimal web server with no globals, which is a small detail that is worth more than it looks. A server built with global state is easier to write and much harder to reason about, and the two examples side by side let you see the difference without a chapter explaining it.

Chapter eight is the email protocol, with a simple email sender, and the sequence of the book has now covered the layers a network program sits on: the socket API, the transport protocols, name resolution, HTTP, and mail.

## TLS and SSH, and the linking notes

The last two chapters with code add the security layers, and both come with a note about how to build them.

Chapter nine uses OpenSSL, and the note says to be sure to link against the OpenSSL libraries when compiling, with the flags `-lssl -lcrypto`. The programs are a report of the installed OpenSSL version, a minimal program that requests a web page over HTTPS, the chapter six HTTP client modified to use HTTPS, the chapter three TCP client modified to use TLS, and a program that prints a certificate from a TLS server.

The certificate program is the one to notice, because printing a server certificate is how you check who you are talking to, and it is the step most tutorial code omits.

Chapter ten is also OpenSSL, with the same linking note, and it takes the earlier servers and turns them into secure ones. The time server from chapter two becomes an HTTPS time server, and the web server from chapter seven becomes an HTTPS web server. The whole chapter is about the fact that adding TLS to an existing server is mostly a matter of swapping the transport underneath, and the diff between the chapter two and chapter ten programs shows exactly how much.

Chapter eleven switches library, using libssh, and the note is to link against the libssh libraries with the flag `-lssh`. The programs report the libssh version, establish a minimal connection, attempt authentication, run a single command over the connection, and download a file over it.

The progression is deliberate. Version, then connection, then authentication, then a command, then a transfer, and the point of the sequence is that each step is a small addition to the last, which is how a protocol like SSH actually feels when you are writing a client for it.

## Timeouts, socket options, and the awkward cases

The final chapter of examples is the one that contains the problems that bite in real programs, and the README describes it in a way that is worth reading twice.

There is a program that shows how to time out a connect call early, and a second program provided for comparison, so the difference between a connect that blocks and a connect that fails fast is visible in two files rather than in a paragraph. There is a program demonstrating the use of SO_REUSEADDR, and a second one for comparison, which is the classic answer to the address already in use error after a restart. There is a program that reports error code descriptions, which turns a numeric return into something you can act on, and a large send program for the case where the data is bigger than the socket buffer.

And there is one that deserves its own mention, a server that purposefully writes to a TCP socket after the client has disconnected. That is not a typo in the README, it is the point of the program. Writing to a closed socket is one of the classic ways a network server dies, because the error arrives asynchronously and arrives after the handler that caused it has returned. A repository that includes a program whose explicit purpose is to crash the server is one that knows what the book is for.

The repository also carries a test directory and a continuous integration workflow, which is more than most book code repositories have, and it means the examples are checked rather than merely published.

For anyone using this as a reference outside the book, the practical advice is to read the chapter that covers the API you need, copy the matching program into your own tree, and then strip it back until only the parts you understand remain. The programs are small enough that it works.

## Conclusion

This repository fits a developer who is working through socket programming in C and wants runnable reference programs for each API, and it fits a teacher who needs small, complete examples that build on three operating systems. It does not fit a production codebase, because these are teaching programs with the simplifications that implies, and it does not fit someone who wants a library. Verify first that the chapter you need has a program for your platform, since the Windows and Unix examples are separated by file in the first chapter, that you have the right development tools for your system, and that you read the linking notes for the chapters that need external libraries, because the TLS and SSH programs need OpenSSL and libssh linked explicitly.

## FAQ

### What is the Hands-On Network Programming with C repository?

It is the example code that accompanies the book of the same name by Lewis Van Winkle, published by Packt, released under the MIT licence. It is organised as one directory per chapter with small complete programs, and the book site is handsonnetworkprogramming.com.

### Which platforms do the example programs compile on?

Unless otherwise noted, all programs compile cleanly on Windows, Linux, and macOS, as C or as C++. The code is tested with MinGW and Visual Studio on Windows, and with gcc and clang on Linux and macOS, and the individual programs that are platform specific are marked as such.

### What does the chapter 3 example do with fork and select?

There is a TCP server that services multiple connections using select and echoes received data back in upper case, and a second server that does the same with fork instead, which is marked as Linux and macOS only. There is also a third server that relays received data to every other connected client.

### Do the TLS examples need extra libraries?

Yes. The examples in the OpenSSL chapters must be linked against the OpenSSL libraries with the ssl and crypto flags, and the SSH examples in the following chapter must be linked against the libssh library. The README gives the exact flags for each.

### Why does the repository have no chapter 12 directory?

Because that chapter does not include any example programs, as the README states. The example directories run from chapter one to chapter eleven and then chapter thirteen, plus a test directory and the continuous integration configuration.

## Sources

- [codeplea/Hands-On-Network-Programming-with-C on GitHub](https://github.com/codeplea/Hands-On-Network-Programming-with-C)
- [Issues](https://github.com/codeplea/Hands-On-Network-Programming-with-C/issues)
- [License: MIT](https://github.com/codeplea/Hands-On-Network-Programming-with-C/blob/master/LICENSE)
- [Project website](https://handsonnetworkprogramming.com/)
- [README](https://github.com/codeplea/Hands-On-Network-Programming-with-C/blob/master/README.md)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/codeplea-hands-on-network-programming-with-c
