Zig
Zig
Move a Connection into Another Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.TakeConnection, which moves the active connection from one Socket into another. The receiving object first releases any connection it previously held.
Background. Unlike TakeSocket, TakeConnection does not add a member to a socket set. After success, the source socket holds no connection. This is useful for handing an accepted connection to a dedicated handler object.
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 listen_sock = try chilkat.Socket.init();
defer listen_sock.deinit();
listen_sock.bindAndListen(5000, 25) catch {
std.debug.print("{s}\n", .{try listen_sock.getLastErrorText(alloc)});
return;
};
const connected_sock = try chilkat.Socket.init();
defer connected_sock.deinit();
listen_sock.acceptNext(20000, connected_sock) catch {
std.debug.print("{s}\n", .{try listen_sock.getLastErrorText(alloc)});
return;
};
// Move the active connection out of connectedSock and into another Socket object. Unlike
// TakeSocket, this does not add a member to a socket set. After success, connectedSock holds no
// connection.
const handler_sock = try chilkat.Socket.init();
defer handler_sock.deinit();
handler_sock.takeConnection(connected_sock) catch {
std.debug.print("{s}\n", .{try handler_sock.getLastErrorText(alloc)});
return;
};
std.debug.print("Connection moved to the handler socket.\n", .{});
}