Hands-On Network Programming with C takes a modern approach to socket programming in the C or C++ programming language. Although the book assumes you are already proficient with C programming, it introduces networking concepts from the ground-up. Care is taken to ensure that programs compile and run on Windows, macOS, and Linux. Additionally, it shows techniques to support both IPv4 and IPv6 from the same program.
After introducing networking fundamentals, the book moves on to examine common protocols. Complete example programs are provided to query DNS servers, download web pages, and send emails. By the middle of the book you will be using cross-platform multiplexing techniques to implement a multi-user web server!
This book doesn't stop there. It then revisits earlier programs to add security. The web client is expanded to support HTTPS, and so is the web server. Even SSH is covered.
The book is rounded out with an important chapter on socket programming "gotchas." This is valuable knowledge gleaned from experience that will keep your programs running well now and into the future.
Ever wonder what the difference is between TCP and UDP? Or exactly how NAT (Network Address Translation) works? What is a port number, anyway? What's data encapsulation, and how does the OSI 7-layer model work? This chapter covers a lot of ground to help you quickly understand networking fundamentals.
Chapter 1 contains these subheadings:
This chapter includes example programs to list the IP addresses of each network adapter.
This chapter lays the foundation for using sockets to send data over a network. You will learn the differences between socket APIs for Windows, macOS, and Linux. The focus is on writing portable code. Techniques are provided for working with both IPv4 and IPv6 the easy way.
Chapter 2 contains these subheadings:
This chapter includes an example program that serves a web page showing the current time — all implemented from scratch in C.
This chapter really dives into TCP sockets and how they work. It introduces a cross-platform technique that lets you use sockets in a non-blocking manner and even manage many simultaneous connections from the same program. This will be crucial to building a multi-user web server in chapter 7.
Chapter 3 contains these subheadings:
The example program in this chapter involves a TCP-based chat server that can handle many concurrent clients.
This chapter takes a deep look at UDP sockets and how to use them. It examines and contrasts different API options for sending and receiving UDP datagrams. It provides a technique for using UDP sockets in a similar way to TCP sockets, and an additional technique that makes UDP socket programming easier is revealed.
Chapter 4 contains these subheadings:
This chapter uses several example programs to illustrate UDP clients and UDP servers.
This chapter builds on chapter 4 by explaining one of the most common UDP protocols — the Domain Name System protocol. DNS is everywhere! It's what resolves a human-readable website name, such as example.com, to the web server address. You used DNS to access this site you're reading right now!
Many networking books gloss over the DNS protocol and are content with using the operating system functions to resolve hostnames. But it's important to understand the protocol on the fundamental level, so this book takes a deep dive into working with DNS at the lowest level.
Chapter 5 contains these subheadings:
This chapter includes example programs to do hostname lookups by calling operating system functions and by sending hand-packed UDP queries to a DNS server. This example program also decodes and interprets the DNS server response.
This chapter introduces HTTP — the web protocol. It shows techniques for downloading web pages from C and focuses on robust, cross-platform code with no external dependencies.
Downloading a web page might not be as easy as you think. Your program will need to create an HTTP request, send it, wait for a response, and parse the HTTP reply. Not every response will use the same encoding, either! Don't worry, though, this chapter shows how to handle HTTP in detail.
Chapter 6 contains these subheadings:
Throughout this chapter, a minimal HTTP client is developed that can download and parse web content. It's all in C, with no dependencies other than your operating system.
This chapter builds on the previous chapter by taking a look at how to build a fully functional web server from the ground up! This is a great project to really bring together all of the socket programming concepts covered so far. A web server needs to handle multiple connections simultaneously, parse HTTP requests, handle multiple file types, and be robust in the face of invalid input from clients. This chapter will work through all those issues and more!
Chapter 7 contains these subheadings:
This chapter develops an HTTP web server example in C that can serve web pages, including images and other binary content. The server uses multiplexing techniques to concurrently support multiple connections.
This chapter looks at SMTP — the email protocol. This protocol is explained in detail, and common problems are addressed before they ever have a chance to hamper your learning. Topics addressed include finding and connecting to an email server, sending emails, encoding attachments, avoiding accidentally getting caught in spam filters, and more.
Chapter 8 contains these subheadings:
This chapter includes an example program in C to send emails over the internet.
This chapter builds on chapter 6 by introducing OpenSSL and working through how SSL/TLS can secure web connections. HTTPS is much like HTTP, except instead of transmitting data directly via TCP, it goes through an encrypted channel instead. This chapter gets you up and running with the basics of securing communications over the internet.
Chapter 9 contains these subheadings:
This chapter includes an example program showing how to download a webpage using HTTPS.
This chapter builds on chapter 7 by adding SSL/TLS support to a web server. The result is a secure web server running over HTTPS. You'll learn not only how to add security to your HTTP server, but also about authentication, certificates, the Server Name Indication (SNI) protocol, and much more.
Chapter 10 contains these subheadings:
As an example program, a secure HTTPS web server is developed in this chapter using the OpenSSL library.
This chapter examines how the Secure Shell (SSH) protocol works and how to use libssh to make SSH and SCP connections. SSH lets you issue commands to a server remotely over the internet, while SCP lets you upload and download files securely. This chapter looks at how to use libssh in practical applications and includes code for using the different available authentication types.
Chapter 11 contains these subheadings:
This chapter includes several example programs. Programs are given to connect, authenticate, issue commands, and download files over SSH.
This chapter talks about monitoring network connections. This is super-useful for debugging your C network programs! For example, what do you do if you get an "Address is already in use" error? How do you know what program is using this address? This chapter addresses the tools, commands, and techniques you'll need to troubleshoot problems as they arise. It even shows how to use Wireshark to record and analyze network traffic.
Chapter 12 contains these subheadings:
As this chapter is all about using tools to monitor and diagnose network issues, there isn't much C programming involved. Nevertheless, these are valuable skills you won't want to miss out on!
If you want to write robust, reliable programs, then this might be the most important chapter in the book. It takes a thorough approach and shows you how to avoid common pitfalls before they become an issue.
This chapter explores how to timeout an inactive connection, how to avoid errors when reusing recently closed sockets, how to avoid crashing your program by writing to a closed socket, how to obtain descriptive error messages for unexpected conditions, and much more.
Ever wonder how to avoid TCP deadlock? Do you know what the Nagle algorithm is and how it can increase or decrease latency? It's all here in chapter 13.
Chapter 13 contains these subheadings:
This chapter includes many example programs to show common errors and how to avoid or resolve them. You don't want to miss this!
The book finishes off with a look at the Internet of Things. IoT devices are becoming more and more common, and this book aims to get you ready for the unique challenges that embedded device programmers face when working over the Internet.
Chapter 14 contains these subheadings:
"Great Example Programs"
...I had my work buy three books on the topic, figuring I'd implement the needed functionality from scratch. This is the only book I ended up using, and I basically just copied the example program. The examples are licensed MIT, so we're allowed to use them. I got it working perfectly within a few hours...Stan E