Sample code for 30+ languages & platforms
Zig

Send BinData Contents over a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.SendBd, which sends bytes from a BinData beginning at an offset. A numBytes of 0 sends from the offset through the end of the data.

Background. The BinData receive methods accumulate raw bytes in a BinData object. A single receive returns only what is currently available, whereas the counted receive waits for an exact number of bytes.

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;
    };

    // Place some bytes into a BinData to be sent.
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.appendString("message payload", "utf-8") catch {};

    // Send bytes from the BinData starting at the given offset.  A numBytes of 0 sends from the offset
    // through the end of the data.
    socket.sendBd(bd, 0, 0) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Sent {d} bytes.\n", .{bd.getNumBytes()});
}