Hands-On Network Programming with C
eBook - ePub

Hands-On Network Programming with C

Learn socket programming in C and write secure and optimized network code

Lewis Van Winkle

  1. 478 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Hands-On Network Programming with C

Learn socket programming in C and write secure and optimized network code

Lewis Van Winkle

Detalles del libro
Vista previa del libro

Información del libro

A comprehensive guide to programming with network sockets, implementing internet protocols, designing IoT devices, and much more with C

Key Features

  • Apply your C and C++ programming skills to build powerful network applications
  • Get to grips with a variety of network protocols that allow you to load web pages, send emails, and do much more
  • Write portable network code for Windows, Linux, and macOS

Book Description

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.

What you will learn

  • 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
  • Explore Simple Mail Transfer Protocol (SMTP) for electronic mail transmission
  • Apply network programming to the Internet of Things (IoT)

Who this book is for

If you're a developer or a system administrator who wants to get started with network programming, this book is for you. Basic knowledge of C programming is assumed.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Hands-On Network Programming with C un PDF/ePUB en línea?
Sí, puedes acceder a Hands-On Network Programming with C de Lewis Van Winkle en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming in C. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.


Computer Science
Programming in C

Section 1 - Getting Started with Network Programming

This section will get the reader up and running with the basics of networking, the relevant network protocols, and basic socket programming.
The following chapters are in this section:
Chapter 1, An Introduction to Networks and Protocols 
Chapter 2, Getting to Grips with Socket APIs 
Chapter 3, An In-Depth Overview of TCP Connections
Chapter 4, Establishing UDP Connections
Chapter 5, Hostname Resolution and DNS

Introducing Networks and Protocols

In this chapter, we will review the fundamentals of computer networking. We'll look at abstract models that attempt to explain the main concerns of networking, and we'll explain the operation of the primary network protocol, the Internet Protocol. We'll look at address families and end with writing programs to list your computer's local IP addresses.
The following topics are covered in this chapter:
  • Network programming and C
  • OSI layer model
  • TCP/IP reference model
  • The Internet Protocol
  • IPv4 addresses and IPv6 addresses
  • Domain names
  • Internet protocol routing
  • Network address translation
  • The client-server paradigm
  • Listing your IP addresses programmatically from C

Technical requirements

Most of this chapter focuses on theory and concepts. However, we do introduce some sample programs near the end. To compile these programs, you will need a good C compiler. We recommend MinGW on Windows and GCC on Linux and macOS. See Appendix B, Setting Up Your C Compiler On Windows, Appendix C, Setting Up Your C Compiler On Linux, and Appendix D, Setting Up Your C Compiler On macOS, for compiler setup.
The code for this book can be found at: https://github.com/codeplea/Hands-On-Network-Programming-with-C.
From the command line, you can download the code for this chapter with the following command:
git clone https://github.com/codeplea/Hands-On-Network-Programming-with-C
cd Hands-On-Network-Programming-with-C/chap01
On Windows, using MinGW, you can use the following command to compile and run code:
gcc win_list.c -o win_list.exe -liphlpapi -lws2_32
On Linux and macOS, you can use the following command:
gcc unix_list.c -o unix_list

The internet and C

Today, the internet needs no introduction. Certainly, millions of desktops, laptops, routers, and servers are connected to the internet and have been for decades. However, billions of additional devices are now connected as well—mobile phones, tablets, gaming systems, vehicles, refrigerators, television sets, industrial machinery, surveillance systems, doorbells, and even light bulbs. The new Internet of Things (IoT) trend has people rushing to connect even more unlikely devices every day.
Over 20 billion devices are estimated to be connected to the internet now. These devices use a wide variety of hardware. They connect over an Ethernet connection, Wi-Fi, cellular, a phone line, fiber optics, and other media, but they likely have one thing in common; they likely use C.
The use of the C programming language is ubiquitous. Almost every network stack is programmed in C. This is true for Windows, Linux, and macOS. If your mobile phone uses Android or iOS, then even though the apps for these were programmed in a different language (Java and Objective C), the kernel and networking code was written in C. It is very likely that the network routers that your internet data goes through are programmed in C. Even if the user interface and higher-level functions of your modem or router are programmed in another language, the networking drivers are still probably implemented in C.
Networking encompasses concerns at many different abstraction levels. The concerns your web browser has with formatting a web page are much different than the concerns your router has with forwarding network packets. For this reason, it is useful to have a theoretical model that helps us to understand communications at these different levels of abstraction. Let's look at these models now.

OSI layer model

It's clear that if all of the disparate devices composing the internet are going to communicate seamlessly, there must be agreed-upon standards that define their communications. These standards are called protocols. Protocols define everything from the voltage levels on an Ethernet cable to how a JPEG image is compressed on a web page. It's clear that, when we talk about the voltage on an Ethernet cable, we are at a much different level of abstraction compared to talking about the JPEG image format. If you're programming a website, you don't want to think about Ethernet cables or Wi-Fi frequencies. Likewise, if you're programming an internet router, you don't want to have to worry about how JPEG images are compressed. For this reason, we break the problem down into many smaller pieces.
One common method of breaking down the problem is to place levels of concern into layers. Each layer then provides services for the layer on top of it, and each upper layer can rely on the layers underneath it without concern for how they work.
The most popular layer system for networking is called the Open Systems Interconnection model (OSI model). It was standardized in 1977 and is published as ISO 7498. It has seven layers:
Let's understand these layers one by one:
  • Physical (1): This is the level of physical communication in the real world. At this level, we have specifications for things such as the voltage levels on an Ethernet cable, what each pin on a connector is for, the radio frequency of Wi-Fi, and the light flashes over an optic fiber.
  • Data Link (2): This level builds on the physical layer. It deals with protocols for directly communicating between two nodes. It defines how a direct message between nodes starts and ends (framing), error detection and correction, and flow control.
  • Network layer (3): The network layer provides the methods to transmit data sequences (called packets) between nodes in different networks. It provides methods to route packets from one node to another (without a direct physical connection) by transferring through many intermediate nodes. This is the layer that the Internet Protocol is defined on, which we will go into in some depth later.
  • Transport layer (4): At this layer, we have methods to reliably deliver variable length data between hosts. These methods deal with splitting up data, recombining it, ensuring data arrives in ord...


  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Section 1 - Getting Started with Network Programming
  8. Introducing Networks and Protocols
  9. Getting to Grips with Socket APIs
  10. An In-Depth Overview of TCP Connections
  11. Establishing UDP Connections
  12. Hostname Resolution and DNS
  13. Section 2 - An Overview of Application Layer Protocols
  14. Building a Simple Web Client
  15. Building a Simple Web Server
  16. Making Your Program Send Email
  17. Section 3 - Understanding Encrypted Protocols and OpenSSL
  18. Loading Secure Web Pages with HTTPS and OpenSSL
  19. Implementing a Secure Web Server
  20. Establishing SSH Connections with libssh
  21. Section 4 - Odds and Ends
  22. Network Monitoring and Security
  23. Socket Programming Tips and Pitfalls
  24. Web Programming for the Internet of Things
  25. Answers to Questions
  26. Setting Up Your C Compiler on Windows
  27. Setting Up Your C Compiler on Linux
  28. Setting Up Your C Compiler on macOS
  29. Example Programs
  30. Other Book You May Enjoy
Estilos de citas para Hands-On Network Programming with C

APA 6 Citation

Winkle, L. V. (2019). Hands-On Network Programming with C (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/969826/handson-network-programming-with-c-learn-socket-programming-in-c-and-write-secure-and-optimized-network-code-pdf (Original work published 2019)

Chicago Citation

Winkle, Lewis Van. (2019) 2019. Hands-On Network Programming with C. 1st ed. Packt Publishing. https://www.perlego.com/book/969826/handson-network-programming-with-c-learn-socket-programming-in-c-and-write-secure-and-optimized-network-code-pdf.

Harvard Citation

Winkle, L. V. (2019) Hands-On Network Programming with C. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/969826/handson-network-programming-with-c-learn-socket-programming-in-c-and-write-secure-and-optimized-network-code-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Winkle, Lewis Van. Hands-On Network Programming with C. 1st ed. Packt Publishing, 2019. Web. 14 Oct. 2022.