Sample code for 30+ languages & platforms
Zig

SSL Client Example

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server, send a simple message, receive a simple response, and disconnect.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const socket = try chilkat.Socket.init();
    defer socket.deinit();

    const ssl = true;
    const max_wait_millisec = 20000;

    // The SSL server hostname may be an IP address, a domain name,
    // or "localhost".  You'll need to change this:
    const ssl_server_host = "123.123.88.88";
    const ssl_server_port = 8123;

    // Connect to the SSL server:
    socket.connect(ssl_server_host, ssl_server_port, ssl, max_wait_millisec) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // Set maximum timeouts for reading an writing (in millisec)
    socket.setMaxReadIdleMs(20000);
    socket.setMaxSendIdleMs(20000);

    // Send a "Hello Server! -EOM-" message:
    socket.sendString("Hello Server! -EOM-") catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // The server (in this example) is going to send a "Hello Client! -EOM-"
    // message.  Read it:
    const received_msg = socket.receiveUntilMatch(alloc, "-EOM-") catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // Close the connection with the server
    // Wait a max of 20 seconds (20000 millsec)
    try socket.close(20000);

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