Book Image

Hands-On Network Programming with C

By : Lewis Van Winkle
Book Image

Hands-On Network Programming with C

By: Lewis Van Winkle

Overview of this book

Network programming enables processes to communicate with each other over a computer network, but it is a complex task that requires programming with multiple libraries and protocols. With its support for third-party libraries and structured documentation, C is an ideal language to write network programs. Complete with step-by-step explanations of essential concepts and practical examples, this C network programming book begins with the fundamentals of Internet Protocol, TCP, and UDP. You’ll explore client-server and peer-to-peer models for information sharing and connectivity with remote computers. The book will also cover HTTP and HTTPS for communicating between your browser and website, and delve into hostname resolution with DNS, which is crucial to the functioning of the modern web. As you advance, you’ll gain insights into asynchronous socket programming and streams, and explore debugging and error handling. Finally, you’ll study network monitoring and implement security best practices. By the end of this book, you’ll have experience of working with client-server applications and be able to implement new network programs in C. The code in this book is compatible with the older C99 version as well as the latest C18 and C++17 standards. You’ll work with robust, reliable, and secure code that is portable across operating systems, including Winsock sockets for Windows and POSIX sockets for Linux and macOS.
Table of Contents (26 chapters)
Title Page
Dedication
About Packt
Contributors
Preface
Index

Chapter 7, Building a Simple Web Server


  1. How does an HTTP client indicate that it has finished sending the HTTP request?

The HTTP request should end with a blank line.

  1. How does an HTTP client know what type of content the HTTP server is sending?

The HTTP server should identify the content with a Content-Type header.

  1. How can an HTTP server identify a file's media type?

A common method of identifying a file's media type is just to look at the file extension. The server is free to use other methods though. When sending dynamic pages or data from a database, there will be no file and therefore no file extension. In this case, the server must know the media type from its context.

  1. How can you tell whether a file exists on the filesystem and is readable by your program? Is fopen(filename, "r") != 0 a good test?

This is not a trivial problem. A robust program will need to consider system specific APIs carefully. Windows uses special filenames that will trip up a program that relies only on fopen() to check for a file's existence.