I want to create a program with a client-server architecture. So I thought of using a local socket to allow for ipc. Here is an example code of what I am doing:
server.zig
const std = @import("std");
pub fn main() !void {
const address = try std.net.Address.initUnix("test.sock");
var server = try address.listen(.{
.reuse_port = true,
.reuse_address = true,
});
defer server.deinit();
std.debug.print("[INFO] Server listening on {}\n", .{address});
var buffer: [512]u8 = undefined;
while (true) {
const connection = try server.accept();
const len = try connection.stream.read(&buffer);
std.debug.print(
"[INFO] Received {d} bytes from client - {s}\n",
.{ len, buffer[0..len] },
);
_ = try connection.stream.write("Hello from server");
connection.stream.close();
}
}
The problem is that the test.sock file remains there after I kill the process.
So if I try and re-run zig run server.zig it fails with error: AddressInUse
How should I handle this?