Sample code for 30+ languages & platforms
Zig

Move a Connection into a Socket Set

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.

Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no 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();

    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 connection out of connectedSock and into a newly created member of this object's socket
    // set.  After one or more such calls, socketSet acts as a set of connected sockets (useful with the
    // SelectForReading / SelectForWriting methods).  After success, connectedSock holds no connection.
    const socket_set = try chilkat.Socket.init();
    defer socket_set.deinit();
    socket_set.takeSocket(connected_sock) catch {
        std.debug.print("{s}\n", .{try socket_set.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Connection added to the socket set.\n", .{});
}