Daemons and Networking

Karel Kubat
karel@kubat.nl / www.kubat.nl

2007

These are the notes on my guest lectures on the course C/C++ 2007, at the State University of Groningen (RuG), Netherlands, where I was graciously asked by Dr. F. B. Brokken to give two closing lectures on a specialized subject. This document is also available in PDF format. The code samples in this document are available in source form. An accompanying presentation is also available in PDF.

Table of Contents

Chapter 1: System Programs

Chapter 2: Daemons

2.1: Forking Daemons

2.1.1: Forking daemon, version 0
2.1.2: Forking daemon, version 1
2.1.3: Forking daemon, alternative version

2.2: Threaded Daemons

2.3: Mixing forking and threaded

2.4: How should I daemonize?

2.5: Scripts, signals and typical daemon behavior

2.5.1: How signals work
2.5.1.1: Defining a signal handler
2.5.1.2: Triggering a signal
2.5.1.3: Sample program

Chapter 3: Networking

3.1: Client-side Socket Handling

3.1.1: How to handle connection timeouts
3.1.2: Putting it all together
3.1.3: Handling connect() timeouts in multithreaded programs

3.2: Socket IO and Timeouts

3.2.1: Looped socket IO
3.2.2: Handling read- and write timeouts

3.3: Server-side Socket Handling

Appendix 1: A TCP port forwarder

1.1: General design of the program

1.2: Technical description and sources

1.2.1: portfw.h: The central header
1.2.2: The main function
1.2.3: Messaging-related functions: error(), warn(), msg()
1.2.3.1: error()
1.2.3.2: warn()
1.2.3.3: msg()
1.2.4: Service-related functions
1.2.4.1: service_start()
1.2.4.2: service_run()
1.2.4.3: service_thread()
1.2.4.4: service_connect()
1.2.4.5: service_stop()
1.2.5: Other support functions
1.2.6: The Makefile