#Writing/Sending isn't working on Windows

1 messages · Page 1 of 1 (latest)

odd dirge
#

This issue is the same between zig 0.13.0 and zig 0.14.0 master

#

I've tested that on 3 different windows computer, with different windows version 10 and 11, and two intel cpu and one amd

odd dirge
#

here is my "init":

self.socket = try std.posix.socket(
            address.any.family, 
            std.posix.SOCK.STREAM | std.posix.SOCK.CLOEXEC | std.posix.SOCK.NONBLOCK, 
            std.posix.IPPROTO.TCP
        );

        try std.posix.setsockopt(
            self.socket,
            std.posix.SOL.SOCKET,
            std.posix.SO.REUSEADDR,
            &std.mem.toBytes(@as(c_int, 1)),
        );

        self.address = undefined;

        var socklen = address.getOsSockLen();
        try std.posix.bind(self.socket, &address.any, socklen);
        try std.posix.listen(self.socket, 64);
        try std.posix.getsockname(self.socket, &self.address.any, &socklen);

odd dirge
#

this isnt making any sense…

crimson basin
#
const std = @import("std");

pub fn main() !void {
    var address = std.net.Address.initIp4(.{ 127, 0, 0, 1 }, 54321);
    const socket = try std.posix.socket(
        address.any.family,
        std.posix.SOCK.STREAM | std.posix.SOCK.CLOEXEC | std.posix.SOCK.NONBLOCK,
        std.posix.IPPROTO.TCP,
    );

    try std.posix.setsockopt(
        socket,
        std.posix.SOL.SOCKET,
        std.posix.SO.REUSEADDR,
        &std.mem.toBytes(@as(c_int, 1)),
    );

    var socklen = address.getOsSockLen();
    try std.posix.bind(socket, &address.any, socklen);
    try std.posix.listen(socket, 64);
    defer std.posix.close(socket);
    try std.posix.getsockname(socket, &address.any, &socklen);
    const client = blk: while (true) {
        const maybe_client = std.posix.accept(socket, null, null, std.posix.SOCK.CLOEXEC) catch {
            std.time.sleep(std.time.ns_per_s * 1);
            continue;
        };
        break :blk maybe_client;
    };
    const bytes_sent = try std.posix.send(client, "Hello!\n", 0);
    std.debug.print("sent {d} bytes.\n", .{bytes_sent});
}
#

output server (only last line is the actual program output, the rest is wine and MESA)

❯ zig build-exe test23.zig -target x86_64-windows-msvc

~/projects/playground via  v14.2.1-gcc via 󰟔 v4.4.1 via  v0.13.0 
❯ wine ./test23.exe
007c:fixme:wineusb:query_id Unhandled ID query type 0x5.
007c:fixme:wineusb:query_id Unhandled ID query type 0x5.
007c:fixme:wineusb:query_id Unhandled ID query type 0x5.
MESA: warning: Support for this platform is experimental with Xe KMD, bug reports may be i
gnored.
sent 7 bytes.
#

output client:

❯ nc 127.0.0.1 54321
Hello!
#

I mostly followed your init, the only difference is that I used std.net.Address to create the address object. I also explicitly closed the socket, but that's because I'm calling listen in main.

#

Which actually makes me want to ask why aren't you using the higher level bindings available in std.net?

odd dirge
#

what i just noticed smth

#

packet = new complete packet ready to be processed
begin_write = it gets to the begin of the sending process
2 280 = just after the std.posix.send it gives me the size of the packet (packet are written in two send, of for the packet header and the others for the packet "payload")

#

the connection destroyed appears 5 seconds after the begin_write

#

so std.posix.send is blocking and only sends the packet when the connection is destroyed... wtf ?