Last active
October 23, 2025 13:08
-
-
Save andrewrk/70e3aeea0afb38ac6caa9b3a1757a4b1 to your computer and use it in GitHub Desktop.
send multiple udp messages to two different recipients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const builtin = @import("builtin"); | |
| const std = @import("std"); | |
| const Io = std.Io; | |
| // Open two netcat instances and make them listen to these ports | |
| const recv1_addr = Io.net.IpAddress.parseIp4("127.0.0.1", 1993) catch unreachable; | |
| const recv2_addr = Io.net.IpAddress.parseIp4("127.0.0.1", 1994) catch unreachable; | |
| const addresses: [2]Io.net.IpAddress = .{ recv1_addr, recv2_addr }; | |
| var gpa_impl: std.heap.DebugAllocator(.{}) = .{}; | |
| const gpa = gpa_impl.allocator(); | |
| pub fn main() !void { | |
| const sender_addr = try Io.net.IpAddress.parseIp4("0.0.0.0", 1992); | |
| var threaded: std.Io.Threaded = .init(gpa); | |
| defer threaded.deinit(); | |
| const io = threaded.io(); | |
| const socket = try sender_addr.bind(io, .{ .mode = .dgram }); | |
| std.debug.print("bound to {f}\n", .{sender_addr}); | |
| while (true) { | |
| std.debug.print("timer running\n", .{}); | |
| // Issue two writes in parallel | |
| var task1 = io.async(Io.net.Socket.send, .{ &socket, io, &addresses[0], "hello\n" }); | |
| defer task1.cancel(io) catch {}; | |
| var task2 = io.async(Io.net.Socket.send, .{ &socket, io, &addresses[1], "hello\n" }); | |
| defer task2.cancel(io) catch {}; | |
| try task1.await(io); | |
| try task2.await(io); | |
| const duration: Io.Clock.Duration = .{ | |
| .raw = .fromSeconds(1), | |
| .clock = .awake, | |
| }; | |
| try duration.sleep(io); | |
| } | |
| std.debug.print("exiting\n", .{}); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const builtin = @import("builtin"); | |
| const std = @import("std"); | |
| const Io = std.Io; | |
| var gpa_impl: std.heap.DebugAllocator(.{}) = .{}; | |
| const gpa = gpa_impl.allocator(); | |
| pub fn main() !void { | |
| const sender_addr = try Io.net.IpAddress.parseIp4("0.0.0.0", 1992); | |
| var threaded: std.Io.Threaded = .init(gpa); | |
| defer threaded.deinit(); | |
| var evented: std.Io.Evented = undefined; | |
| try evented.init(gpa, .{ .n_threads = 1 }); | |
| defer evented.deinit(); | |
| const io = evented.io(); | |
| const stream = try sender_addr.connect(io, .{ .mode = .stream }); | |
| var reader1_buffer: [32]u8 = undefined; | |
| var reader1 = stream.reader(io, &reader1_buffer); | |
| var reader2_buffer: [32]u8 = undefined; | |
| var reader2 = stream.reader(io, &reader2_buffer); | |
| while (true) { | |
| var task1 = io.async(Io.Reader.takeDelimiter, .{ &reader1.interface, '\n' }); | |
| defer _ = task1.cancel(io) catch {}; | |
| var task2 = io.async(Io.Reader.takeDelimiter, .{ &reader2.interface, '\n' }); | |
| defer _ = task2.cancel(io) catch {}; | |
| std.debug.print("task1 read '{?s}'\n", .{try task1.await(io)}); | |
| std.debug.print("task2 read '{?s}'\n", .{try task2.await(io)}); | |
| const duration: Io.Clock.Duration = .{ | |
| .raw = .fromSeconds(1), | |
| .clock = .awake, | |
| }; | |
| try duration.sleep(threaded.io()); | |
| } | |
| std.debug.print("exiting\n", .{}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment