#How to make a unix socket in zig?

1 messages · Page 1 of 1 (latest)

mystic flare
#

Hi,
I have been looking this online but can't find specific for zig.
Tried to do it on but checking the docs of std but Im having an error error.AddressInUse.

const Server = std.net.StreamServer;

pub fn init() !void {
    var server = Server.init(.{
        .reuse_port = true,
        .reuse_address = true,
    });
    defer server.deinit();

    const path = "/tmp/project/";

    const address = try std.net.Address.initUnix(path);
    try server.listen(address);

    var buff: [500]u8 = undefined;
    while (true) {
        const conn = try server.accept();

        _ = try conn.stream.read(&buff);
    }
}

is there a simple example I can check from to learn more about this?

lofty forum
#

unix sockets are files, not directories

#

also it has to not exist yet

#

as in you probably need to delete the old one first

mystic flare
#
const path = "/tmp/project/.file.sock";
    
const tmp_dir = try std.fs.openDirAbsolute("/tmp", .{});
    
const socket_dir = try tmp_dir.makeOpenPath("project", .{});
    
const file = try socket_dir.createFile(".file.sock", .{});
file.close();
#

this is the code that I have there

#

had just removed for the sake of simplicity sorry

lofty forum
#

yeah, you can't create a socket over a regular (createFile'd) file

mystic flare
#

hmm

#

how to do it?

lofty forum
#

you need to use a path where the parent dir exists but the file does not

mystic flare
#

yes but should I create the file since I can't connect just by passing the directory?

lofty forum
#

you pass a non existing path and listen creates the file

#

if the path exists already, then listen will fail

mystic flare
#

it says error.FileNotFound

lofty forum
#

what does, I can't do much without knowing what the code is

mystic flare
#

well

#

will copy the whole code

#
const std = @import("std");
const fs = std.fs;
const net = std.net;
const Server = net.StreamServer;

pub fn init() !void {
    var server = Server.init(.{
        .reuse_port = true,
        .reuse_address = true,
    });
    defer server.deinit();

    const path = "/tmp/aestuarium/.aestuarium.sock";

    const address = try std.net.Address.initUnix(path);
    try server.listen(address);

    var buff: [500]u8 = undefined;
    while (true) {
        const conn = try server.accept();

        const bytes = try conn.stream.read(&buff);
        _ = bytes; // autofix
    }
}
lofty forum
#

that means that /tmp/aestuarium doesn't exist as a directory

mystic flare
#

yep

#

deleted it

lofty forum
#

which is why the code is erroring

#

you can't create a file in a dir that doesn't exist

#
$ ls -l /tmp/aestuarium/.aestuarium.sock 
srwxr-xr-x 1 user group 0 Feb 18 20:26 /tmp/aestuarium/.aestuarium.sock
#

didn't change the code at all

#

s means socket of course

mystic flare
#

oh

#

understand what you mean

#

there should be the directory but the .sock file should not exist

#

yep it worked

lofty forum
#

yes, just like if you were creating a file