Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Transfer a File using Sockets (TLS or non-TLS)

See more Socket/SSL/TLS Examples

Demonstrates how to two programs, one a socket writer and the other a socket reader, can transfer a file. The connection can be TLS or a regular non-encrypted TCP connection.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // On the sending side, we'll load the file into a BinData object and send.
    // On the receiving side, we'll read from the socket connection into a BinData, and save to a file.
    // This example assumes the file is not crazy-large, and that the entire contents
    // can fit into memory.
    // (If the file is too large for memory, there are other ways to send. It just involves streaming or
    // sending the file chunk-by-chunk..)

    // This section of code is for the sender.
    const bd_to_send = try chilkat.BinData.init();
    defer bd_to_send.deinit();
    try bd_to_send.loadFile("somePath/someFile.dat");
    // Assume success for the example...

    const snd_sock = try chilkat.Socket.init();
    defer snd_sock.deinit();
    const b_use_tls = true;
    const port = 5555;
    var max_wait_ms: i32 = 5000;
    try snd_sock.connect("some_domain_or_ip.com", port, b_use_tls, max_wait_ms);
    // Assume success for the example...

    // Tell the receiver how many bytes are coming.
    const num_bytes = bd_to_send.getNumBytes();
    const b_big_endian = true;
    try snd_sock.sendInt32(num_bytes, b_big_endian);

    // Send the file data (sends the entire contents of bdToSend).
    try snd_sock.sendBd(bd_to_send, 0, 0);

    // Get an acknowledgement.
    snd_sock.receiveInt32(b_big_endian) catch {
        std.debug.print("{s}\n", .{try snd_sock.getLastErrorText(alloc)});
        return;
    };

    // Did the receiver get the correct number of bytes?
    if (snd_sock.getReceivedInt() != num_bytes) {
        std.debug.print("The receiver did not acknowledge with the correct number of bytes.\n", .{});
        return;
    }

    std.debug.print("File sent!\n", .{});

    // ------------------------------------------------------------------------------------
    // The code below is for the receiving side (running on some other computer..)

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

    listen_sock.bindAndListen(5555, 25) catch {
        std.debug.print("{s}\n", .{try listen_sock.getLastErrorText(alloc)});
        return;
    };

    // Get the next incoming connection
    // Wait a maximum of 20 seconds (20000 millisec)
    const rcv_sock = try chilkat.Socket.init();
    defer rcv_sock.deinit();
    listen_sock.acceptNext(20000, rcv_sock) catch {
        std.debug.print("{s}\n", .{try listen_sock.getLastErrorText(alloc)});
        return;
    };

    // The sender will first send the big-endian integer for the number of bytes
    // that are forthcoming..
    rcv_sock.receiveInt32(b_big_endian) catch {
        std.debug.print("{s}\n", .{try rcv_sock.getLastErrorText(alloc)});
        return;
    };

    const num_bytes_coming = rcv_sock.getReceivedInt();

    // Receive that many bytes..
    const bd_received = try chilkat.BinData.init();
    defer bd_received.deinit();
    rcv_sock.receiveBdN(@as(u32, @intCast(num_bytes_coming)), bd_received) catch {
        std.debug.print("{s}\n", .{try rcv_sock.getLastErrorText(alloc)});
        return;
    };

    // Acknowledge the sender by sending back the number of bytes we received.
    try rcv_sock.sendInt32(bd_received.getNumBytes(), b_big_endian);

    // Close the connection.
    max_wait_ms = 20;
    rcv_sock.close(max_wait_ms) catch {};

    // Save the received data to a file.
    try bd_received.writeFile("somePath/someFile.dat");
    // Assume success for the example...

    std.debug.print("File received!\n", .{});
}