List of examples
This folder contains Rust network programming examples referred to in the course material, each in separate Rust project folder. They contain Cargo.toml, etc. required files so that you can build and test them using cargo build and cargo run.
One easy way to try the examples is that you clone the course repository to your machine and locate this directory. The examples contain Cargo.toml, etc. required files so that you can build and test them using cargo build and cargo run. Feel free to modify the examples as you want, to try different things.
The examples are as follows:
Basic socket operations
simple-client: Opens connection, then writes and reads a bit of data.
tcpheader Example of converting a struct consisting TCP header fields into byte stream that can be written to a socket, and conversely, filling the struct from data read from byte stream.
send-much: Contains both simple server and client implementation: server accepts connections, then waits for user input and reads all data someone sends to the socket, until the socket is closed. Client just sends the requested number of bytes. Used to demonstrate the effect of socket buffering on socket API behavior.
simple-udp: Simple UDP example where client reads input from user and sends it to a UDP server, then reads a response back. Server waits for incoming datagrams, and echoes them back to client. Both client and server implementations are in the same source in their respective functions.
Project template
- project-template: An example project template of a Rust workspace divided into two Rust packages for client and server applications. It also shows how the Slint UI framework is used for window-based client GUI.
Different server variants
simple-server: Accepts a connection, then reads data from socket and writes some data back, then closes the connection. Handles only one connection at a time in a loop. Can be tested together with simple-client.
iterative-server: Accepts incoming connections, then waits for incoming data and echoes it back in all active connections. Keeps connections open until other end closes them. Can handle multiple connections in parallel. Demonstrates non-blocking sockets in an iterative single-threaded server using Rust’s mio crate.
threaded-server: Similar to iterative-server, but spawns a new thread for each active client.
async-server: Similar to threaded-server but applies asynchronous operations using Rust’s tokio crate.
Testing
- integration-test: A length-prefixed TCP echo service with an integration test that starts a server thread, sends a message over localhost, and checks the response and connection closure. Run with
cargo test.
TLS
There are client and server examples for testing Transport Layer Security. They are similar to above examples, but use TLS with rustls crate. They can be tested together using the local certificates provided.
tls-client: Opens TLS session and sends message given as command line argument. You can test it with local server (at localhost) by giving the CA certificate located in
certdirectory, or a public server (e.g. HTTPS towww.aalto.fi) by not giving the CA. See examples at the beginning ofmain.rs.tls-server: Modified from the async-server example based on Tokio, but now using TLS with tokio-rustls crate. The needed certificate and private key are located in the
certdirectory, which you need to give as command line argument.
GUI and audio examples
macroquad: Simple example demonstrating a moving and rotating object using the Macroquad library, as controlled by arrow keys.
audio-streaming: demonstrates use of CPAL crate to sample audio into UDP datagrams, and play audio from arriving UDP datagrams.