Skip to content

windows: std.net.getAddressList requires manual WSAStartup call #8943

@zacwood9

Description

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

pub fn main() anyerror!void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = &arena.allocator;

    const list = try std.net.getAddressList(allocator, "ziglang.org", 443);
    std.log.info("{}", .{list.addrs[0]});
}

This program crashes with the error error.Unexpected: GetLastError(10093): Either the application has not called WSAStartup, or WSAStartup failed.. To get this to work you need to add a call to WSAStartup as follows:

const std = @import("std");

pub fn main() anyerror!void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = &arena.allocator;

    _ = try std.os.windows.WSAStartup(2, 2);

    const list = try std.net.getAddressList(allocator, "google.com", 443);
    std.log.info("{}", .{list.addrs[0]});

    try std.os.windows.WSACleanup();
}

The os.socket API abstracts this call away from user-code through the WSASocketW in os/windows.zig, which initializes WSA if needed. It seems right to me that the std.net APIs should do the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.os-windowsMicrosoft Windowsstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions