Zig Cookbook

Introduction

Listen on unused port TCP/IP

In this example, the port is displayed on the console, and the program will listen until a request is made. Ip4Address assigns a random port when setting port to 0.

//! Start an echo TCP server at an unused port.
//!
//! Test with
//! echo "hello zig" | nc localhost <port>

const std = @import("std");
const net = std.net;
const print = std.debug.print;

pub fn main() !void {
    const loopback = try net.Ip4Address.parse("127.0.0.1", 0);
    const localhost = net.Address{ .in = loopback };
    var server = try localhost.listen(.{
        .reuse_address = true,
    });
    defer server.deinit();

    const addr = server.listen_address;
    print("Listening on {}, access this port to end the program\n", .{addr.getPort()});

    const client = try server.accept();
    // In real world, you'd want to handle multiple clients, probably in separate threads.
    try handleClient(client);
}

fn handleClient(client: net.Server.Connection) !void {
    print("Accepted connection from {f}\n", .{client.address});
    defer client.stream.close();
    var stream_buf: [1024]u8 = undefined;
    var reader = client.stream.reader(&stream_buf);
    // Here we echo back what we read directly, so the writer buffer is empty
    var writer = client.stream.writer(&.{});

    while (true) {
        print("Waiting for data from {f}...\n", .{client.address});
        const msg = reader.interface().takeDelimiterInclusive('\n') catch |err| {
            if (err == error.EndOfStream) {
                print("{f} closed the connection\n", .{client.address});
                return;
            } else {
                return err;
            }
        };
        print("{f} says {s}", .{ client.address, msg });
        try writer.interface.writeAll(msg);
        // No need to flush, as writer buffer is empty
    }
}

When start starts up, try test like this:

echo "hello zig" | nc localhost <port>

By default, the program listens with IPv4. If you want IPv6, use ::1 instead of 127.0.0.1, replace net.Ip4Address.parse by net.Ip6Address.parse and the field .in in the creation of the net.Address with .in6.

(And connect to something like ip6-localhost, depending on the way your machine is set up.)

The next section will show how to connect to this server using Zig code.