Zig Cookbook

Introduction

UDP Echo

Similar to the TCP server example, this program will listen on the specified IP address and port, but for UDP datagrams this time. If data is received, it will be echoed back to the sender’s address.

Although std.Io.net is mostly focused on abstractions for TCP (so far), we can still make use of socket programming to communicate via UDP.

//! Start a UDP echo on an unused port.
//!
//! Test with
//! echo "hello zig" | nc -u localhost <port>

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

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    // adjust the ip/port here as needed
    const addr = try net.IpAddress.parse("127.0.0.1", 32100);

    // Bind a UDP socket
    const sock = try addr.bind(io, .{ .mode = .dgram, .protocol = .udp });
    defer sock.close(io);

    var buf: [1024]u8 = undefined;

    print("Listen on {f}\n", .{addr});

    // we did not set the NONBLOCK flag, so the program will wait until data is received
    const msg = try sock.receive(io, &buf);
    print(
        "received {d} byte(s) from {f};\n    string: {s}\n",
        .{ msg.data.len, msg.from, msg.data },
    );

    try sock.send(io, &msg.from, msg.data);
    print("echoed {d} byte(s) back\n", .{msg.data.len});
}

After starting the program, test as follows with nc, using the -u flag for UDP:

echo "hello zig" | nc -u localhost <port>
Previous: TCP Client
Next: GET