Zig
Zig
Receive Bytes as Encoded Text from a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveBytesENC, which receives one delivery of binary data and returns it encoded as text using a named encoding.
Background. The ENC methods move raw binary data to and from printable text using a named encoding such as
base64 or hex, which is convenient for logging or for handling binary data as strings.Chilkat Zig Downloads
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;
};
// Receive one delivery of binary data and return it encoded as text using the named encoding. This
// is convenient for logging or transporting binary data as printable text.
const encoded = socket.receiveBytesENC(alloc, "base64") catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{encoded});
}