Checking if we are connected to the Internet

Hi @bilva_patra welcome to ziggit :slight_smile:

Technically what is the meaning of “Connected to internet”?
For most people not connected mean “when I cannot google”.
With that definition:

const std = @import("std");

pub fn connected(allocator: std.mem.Allocator) bool {
    const s = std.net.tcpConnectToHost(allocator, "google.com", 443) catch return false;
    s.close();
    return true;
}

test "connected to internet" {
    try std.testing.expect(connected(std.testing.allocator));
}
6 Likes