Sample code for 30+ languages & platforms
Zig

Receive a Block of Bytes to a File

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBytesToFile, which receives one available block of bytes and appends it to a file.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Any buffered data is written first; otherwise a single receive operation is performed. The method appends, so repeated calls accumulate received blocks in the file.

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();

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

    // Connect to the server using TLS.
    const b_tls = true;
    const max_wait_ms = 5000;
    socket.connect("example.com", 5000, b_tls, max_wait_ms) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // The file path is relative to the application's current working directory.  An absolute path
    // may also be used.  Supply the path appropriate to your own environment.

    // Receive one available block of bytes and append it to a file.  If bytes are already buffered,
    // they are used first; otherwise a single receive operation is performed.
    socket.receiveBytesToFile("qa_output/received.dat") catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Received block appended to the file.\n", .{});
}