Zig
Zig
Socket Send/Receive 16-bit Integers
Demonstrates sending and receiving 16-bit integers over a socket connection using either big-endian or little-endian byte ordering.Chilkat Zig Downloads
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.
const sock = try chilkat.Socket.init();
defer sock.deinit();
// --------------------------------------------------------------------
// This example uses the public TCP echo service at https://tcpbin.com/
// --------------------------------------------------------------------
const use_tls = false;
const port = 4242;
const max_wait_ms = 5000;
sock.connect("tcpbin.com", port, use_tls, max_wait_ms) catch {
std.debug.print("{s}\n", .{try sock.getLastErrorText(alloc)});
return;
};
// Wait a max of 2 seconds for a response..
sock.setMaxReadIdleMs(2000);
// Send a 16-bit integer using big-endian byte ordering (also called network byte order)
// 12022 decimal = 2EF6 hex
var value: i32 = 12022;
var big_endian: bool = true;
sock.sendInt16(value, big_endian) catch {};
// Send it again, because we'll read it two different ways..
sock.sendInt16(value, big_endian) catch {};
// The tcpbin.com echo server only echoes after receiving an LF (linefeed char)
sock.sendByte(10) catch {};
// Let's see hex values of the 2 bytes sent in network byte order (big-endian)
// (The echo server sends back exactly the bytes received.)
var hex_str: [:0]const u8 = try sock.receiveNBytesENC(alloc, 2, "hex");
// In the big-ending byte order, the byte order is most significant byte to least significant,
// therefore we should see the bytes in the order 0x2E 0xF6.
std.debug.print("Expecting big-endian 2EF6\n", .{});
std.debug.print("{s}\n", .{hex_str});
// Let's read directly to an integer..
try sock.receiveInt16(big_endian, true);
std.debug.print("Expecting 12022\n", .{});
std.debug.print("Received: {d}\n", .{sock.getReceivedInt()});
// Consume the LF that gets echoed back..
sock.receiveByte(true) catch {};
// --------------------------------------------------------------------
// Let's do the same thing, but with little-endian byte ordering.
std.debug.print("----\n", .{});
// 12022 decimal = 2EF6 hex, but little-endian byte ordering will send the bytes in the order 0xF6 0x2E
value = 12022;
big_endian = false;
sock.sendInt16(value, big_endian) catch {};
sock.sendInt16(value, big_endian) catch {};
sock.sendByte(10) catch {};
hex_str = try sock.receiveNBytesENC(alloc, 2, "hex");
std.debug.print("Expecting little-endian F62E\n", .{});
std.debug.print("{s}\n", .{hex_str});
try sock.receiveInt16(big_endian, true);
std.debug.print("Expecting 12022\n", .{});
std.debug.print("Received: {d}\n", .{sock.getReceivedInt()});
// Consume the LF that gets echoed back..
sock.receiveByte(true) catch {};
sock.close(1000) catch {};
// Output:
// Expecting big-endian 2EF6
// 2EF6
// Expecting 12022
// Received: 12022
// ----
// Expecting little-endian F62E
// F62E
// Expecting 12022
// Received: 12022
}