Skip to content

Tools and Your First Network Program

Build a small program before opening a socket. You will use the same CMake workflow throughout the Network course.

Before you start

Use a terminal on the Pi with Raspberry Pi OS or another Linux distribution. You need basic C++ functions, strings and exceptions; the Modern C++ course covers these. No wiring or administrator access is needed to run the examples.

Outcome: build a C++20 executable and run the network checks. All commands on this page run on the Pi.

Prepare the tools

Inspect the tools first:

1
2
3
4
5
6
c++ --version
cmake --version
python3 --version
git --version
openssl version
pkg-config --modversion openssl

The project needs a C++20 compiler, CMake 3.28 or later, OpenSSL 3.0 or later with development headers, and Python 3.11 or later with its ssl module for tests. OpenSSL is used in the later TLS chapter; the first programs still call Linux sockets directly. The HTTP fixture uses Python's protocol-selection option. If the tools are missing on Raspberry Pi OS, package installation is an administrative change; review its prompt:

sudo apt update
sudo apt install build-essential cmake python3 git libssl-dev pkg-config

Get a fresh practice checkout

Create a private directory and clone the examples. Keep the path printed by pwd; later lessons run from this category directory.

1
2
3
4
network_workspace=$(mktemp -d "$HOME/network-labs.XXXXXX") || exit 1
git clone https://github.com/ohyaan/with-raspberry-pi-examples.git "$network_workspace/examples"
cd "$network_workspace/examples/network" || exit 1
pwd

The repository contains other courses, but each category has its own build. Configure and build only network/:

1
2
3
cmake --preset native-debug
cmake --build --preset native-debug --parallel 2
./build/native/debug/hello_network

Expected output:

Network lab ready
C++20: yes

Read the first program

The complete file is app/hello-network/main.cpp:

1
2
3
4
5
6
#include <iostream>

int main() {
    static_assert(__cplusplus >= 202002L, "Build this course as C++20");
    std::cout << "Network lab ready\nC++20: yes\n";
}

static_assert checks the language mode at compile time. CMake requests C++20 and rejects an unsupported compiler. Printing this line establishes that the build works; it does not test network access.

Run the checks

ctest --preset native-debug --parallel 2

The current suite has 629 checks covering UDP, TCP connections, framing, deadlines, multiple clients, HTTP clients and servers, chunked bodies, connection reuse, verified TLS, HTTPS and WebSocket text/control exchanges and bounded extended lengths, message fragmentation, verified WSS, DTLS and operation-ID retries. They use temporary loopback ports and finish automatically, including three-second I/O-timeout checks. The TLS checks generate disposable practice certificates in private temporary directories; they do not change system trust. Running the checks requires no root access, internet service, firewall change or permanent process. A restricted execution environment that forbids sockets cannot run these communication checks.

To repeat with optimization:

1
2
3
cmake --preset native-release
cmake --build --preset native-release --parallel 2
ctest --preset native-release --parallel 2

If something differs

  • Preset not found: run from examples/network, not the repository root.
  • CMake version too old: check the installed version before proceeding.
  • Compile error at static_assert: check the compiler and CMake configuration.
  • Operation not permitted when creating sockets: inspect the container or sandbox restrictions. Port 9000 does not require root.

Check your understanding

Does a successful build prove that a server on another computer can be reached?

Compare your reasoning

No. It checks compilation and linking. The loopback tests exercise local communication; a remote connection has additional addressing, routing and access conditions.

Continue the course

Course overview · Next: Addresses, Ports and Sockets

Donate