Skip to content

Quick Start EN

b23r0 edited this page Oct 12, 2026 · 4 revisions

English · 简体中文

Quick start

Minimum requirements: Rust 1.85 and Tokio 1.38. The library uses Rust edition 2024. Create examples and run services in a task-specific copy, container or VM with private Cargo caches and build output. Place local services in a private network namespace.

1. Add dependencies

Create a binary project and add these dependencies to Cargo.toml:

[package]
name = "raknet-echo"
version = "0.1.0"
edition = "2024"

[dependencies]
rust-raknet = "1.4.0"
tokio = { version = "1.38", features = ["full"] }

These snippets target published 1.4.0. Rust 1.85 is the declared MSRV; tokio = "1.38" is the declared dependency range, not a guarantee that every freshly resolved dependency supports the MSRV. Preserve a compatible lockfile when using older Rust. Send policy, recovery policy and blocking calls are optional.

2. Echo server

Save this as src/bin/server.rs:

use rust_raknet::{RaknetListener, Reliability, error::Result};

#[tokio::main]
async fn main() -> Result<()> {
    let address = "127.0.0.1:19132".parse().unwrap();
    let mut listener = RaknetListener::bind(&address).await?;
    listener.listen().await;

    loop {
        let socket = listener.accept().await?;
        tokio::spawn(async move {
            while let Ok(message) = socket.recv().await {
                if let Err(error) = socket
                    .send(&message, Reliability::ReliableOrdered)
                    .await
                {
                    eprintln!("Echo failed: {error}");
                    break;
                }
            }
            let _ = socket.close().await;
        });
    }
}

bind() creates the UDP socket, listen() starts the listener, and accept() returns a session. Each session runs in its own asynchronous task so the accept loop can keep handling new connections.

3. Client

Save this as src/bin/client.rs:

use rust_raknet::{RaknetSocket, Reliability, error::Result};

#[tokio::main]
async fn main() -> Result<()> {
    let address = "127.0.0.1:19132".parse().unwrap();
    let socket = RaknetSocket::connect(&address).await?;
    socket.send(&[0xfe, 1, 2, 3], Reliability::ReliableOrdered).await?;
    let reply = socket.recv().await?;
    println!("Echo: {reply:?}");
    socket.close().await
}

Run these in separate terminals sharing the same isolated network:

cargo run --bin server
cargo run --bin client

The client should print Echo: [254, 1, 2, 3]. Both endpoints must share a network namespace. 127.0.0.1 in one container usually does not refer to another container.

4. Repository examples

The server.rs and client.rs above provide the basic echo example. The repository also includes concurrency, concurrency_benchmark, sharded_echo, interop and bedrock_probe. Check the examples directory for arguments and source.

Ordinary .rs examples use cargo run --example NAME. examples/proxy, examples/bedrock_ping and examples/test_benchmark are separate Cargo projects and use --manifest-path.

Next: Choose a reliability mode · Messages and backpressure

Blocking API · API index · Errors and deadlines

Clone this wiki locally