#unix socket on Windows, wth? why not?

1 messages · Page 1 of 1 (latest)

deft stone
#

I am on Windows 11

const std = @import("std");
const builtin = @import("builtin");

const fs = std.fs;
const net = std.net;

const is_windows = builtin.os.tag == .windows;

pub fn main() !void {
    std.debug.print("START\n", .{});
    if (!net.has_unix_sockets) {
        std.debug.print("This system does not support Unix domain sockets\n", .{});
        std.process.exit(1);
    }

    // var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    // defer _ = gpa.deinit();
    // const allocator = gpa.allocator();

    const socket_path = if (is_windows)
        "\\\\.\\pipe\\example-socket"
    else
        "/tmp/example.sock";

    std.debug.print("get addr\n", .{});
    const address = try net.Address.initUnix(socket_path);
    // std.debug.print("unix socket addr: {s}\n", .{address.path});

    std.debug.print("listen call\n", .{});
    var srv = try address.listen(.{
        //.kernel_backlog = 2,
        // .reuse_address = true,
        // .reuse_port = true,
        //.force_nonblocking = true,
    });

    if (!is_windows) {
        fs.deleteFileAbsolute(socket_path) catch |err| {
            if (err != error.FileNotFound) {
                return err;
            }
        };
    }

    srv.deinit();

    std.debug.print("LISTENING\n", .{});
}

output

PS C:\Users\pseud\zigzog> zig build
PS C:\Users\pseud\zigzog> .\zig-out\bin\zigzog.exe
START
get addr
listen call
error: NetworkSubsystemFailed
C:\Users\pseud\zig\lib\std\posix.zig:3645:33: 0xdf3b15 in bind (zigzog.exe.obj)
.WSAENETDOWN => return error.NetworkSubsystemFailed,
^
C:\Users\pseud\zig\lib\std\net.zig:263:9: 0xdf1dcc in listen (zigzog.exe.obj)
try posix.bind(sockfd, &address.any, socklen);
^
C:\Users\pseud\zigzog\src\main.zig:51:15: 0xdf14c2 in main (zigzog.exe.obj)
var srv = try address.listen(.{


wth ? Why ? I don't get this..
#

And the stupid post length wouldn't let me state this, , but
From std.net.zig:

pub const has_unix_sockets = switch (native_os) {
    .windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false,
    else => true,
};

It should be possible, as I understand it.

serene lily
#

unix sockets are distinct from named pipes, a unix socket must still be a path to a directory on the filesystem (or start with a null byte and be an abstract socket)

deft stone
#

That was it.
I mean.. I know that from Linux, I just.. took crap advice 😄

Hey, thanks