-
Book Overview & Buying
-
Table Of Contents
Systems Programming with Zig
By :
While binding to 0.0.0.0 is the pragmatic solution for most cases, there are situations where you want explicit control over which addresses your server listens on — for example, exposing a service on an internal network interface while keeping it off a public-facing one. In those cases, you need one listener per address, since each listen() call binds to exactly one IP. The approach is to store your target addresses in a fixed array, then iterate over them: for each address, you call address.listen() to get a server, print a confirmation, and spawn a dedicated std.Thread to run the accept loop. The thread handles are stored in a parallel array so the main function can call join() on each one at the end, blocking until all listeners shut down. The accept loop itself is factored out into a serveLoop() function that takes ownership of the server and an allocator, runs accept() in a while(true) loop, and handles each connection — catching...