Hands-On Network Programming with C

Written By Lewis Van Winkle

About the book

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.

View the back cover

What you will learn

  • Learn low-level socket and network concepts from the ground up.
  • Use C to create clean, portable code for Windows, macOS, and Linux.
  • Learn the easy way to support both IPv4 and IPv6.
  • Download a web page using only a TCP socket and an understanding of HTTP.
  • Learn about designing C programs for embedded devices and the Internet of Things.
  • Perform a manual DNS query and parse the response.
  • Implement a portable, multi-user web server from scratch.
  • Secure your client and server communications with HTTPS.
  • Learn how to avoid the dreaded TCP deadlock.
  • Send an email from scratch using a direct connection to a mail-server.

Table of Contents

  • 1. Introducing Networks and Protocols
  • 2. Getting to Grips with Socket APIs
  • 3. An In-Depth Overview of TCP Connections
  • 4. Establishing UDP Connections
  • 5. Hostname Resolution and DNS
  • 6. Building a Simple Web Client
  • 7. Building a Simple Web Server
  • 8. Making Your Program Send Email
  • 9. Loading Secure Web Pages with HTTPS and OpenSSL
  • 10. Implementing a Secure Web Server
  • 11. Establishing SSH Connections with libssh
  • 12. Network Monitoring and Security
  • 13. Socket Programming Tips and Pitfalls
  • 14. Web Programming for the Internet of Things

Introducing Networks and Protocols

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:

  • The internet and C
  • OSI layer model
  • TCP/IP layer model
  • Data encapsulation
  • Internet Protocol
  • Domain names
  • Internet routing
  • Port numbers
  • Clients and servers
  • Putting it together
  • What's your address?
  • Listing network adapters from C

This chapter includes example programs to list the IP addresses of each network adapter.

Getting to Grips with Socket APIs

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:

  • What are sockets?
  • Socket setup
  • Two types of sockets
  • Socket functions
  • Anatomy of a socket program
  • Berkeley sockets versus Winsock sockets
  • Our first program

This chapter includes an example program that serves a web page showing the current time — all implemented from scratch in C.

An In-Depth Overview of TCP Connections

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:

  • Multiplexing TCP connections
  • Synchronous multiplexing with select()
  • A TCP client
  • A TCP server
  • Blocking on send()
  • TCP is a stream protocol

The example program in this chapter involves a TCP-based chat server that can handle many concurrent clients.

Establishing UDP Connections

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:

  • How UDP sockets differ
  • A first UDP client/server
  • A UDP server

This chapter uses several example programs to illustrate UDP clients and UDP servers.

Hostname Resolution and DNS

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:

  • How hostname resolution works
  • Name/address translation functions
  • The DNS protocol
  • A DNS query program

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.

Building a Simple Web Client

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:

  • The HTTP protocol
  • What's in a URL
  • Implementing a web client
  • HTTP POST requests

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.

Building a Simple Web Server

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:

  • The HTTP server
  • Content types
  • Creating the server socket
  • Multiple connections buffering
  • The main loop
  • Security and robustness

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.

Making Your Program Send Email

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:

  • Email servers
  • SMTP dialog
  • The format of an email
  • A simple SMTP client program
  • Enhanced emails
  • Spam-blocking pitfalls

This chapter includes an example program in C to send emails over the internet.

Loading Secure Web Pages with HTTPS and OpenSSL

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:

  • HTTPS overview
  • Encryption basics
  • The TLS protocol
  • OpenSSL
  • A simple HTTPS client
  • Other examples

This chapter includes an example program showing how to download a webpage using HTTPS.

Implementing a Secure Web Server

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:

  • HTTPS and OpenSSL summary
  • HTTPS server with OpenSSL
  • HTTPS server challenges

As an example program, a secure HTTPS web server is developed in this chapter using the OpenSSL library.

Establishing SSH Connections with libssh

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:

  • The SSH protocol
  • libssh
  • SSH authentication
  • Executing a remote command
  • Downloading a file

This chapter includes several example programs. Programs are given to connect, authenticate, issue commands, and download files over SSH.

Network Monitoring and Security

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:

  • The purpose of network monitoring
  • Testing reachability
  • Checking local connections
  • Snooping on connections
  • Network security
  • Network-testing etiquette

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!

Socket Programming Tips and Pitfalls

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:

  • Error handling
  • TCP socket tips
  • Socket's local address
  • Multiplexing with a large number of sockets

This chapter includes many example programs to show common errors and how to avoid or resolve them. You don't want to miss this!

Web Programming for the Internet of Things

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:

  • What is the IoT?
  • Connectivity options
  • Hardware choices
  • External transceivers and modems
  • IoT protocols
  • Firmware updates
  • Ethics of IoT

HONPC is Rated 5 STARS on Amazon

"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

Order Now

Purchase on Amazon Purchase on Packt