Zig
Zig
Accept the Next Incoming Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.AcceptNext, which waits for and accepts the next incoming connection on a listening socket, delivering it as a new connected Socket.
Background. A maxWaitMs of 0 waits indefinitely; a positive value waits up to that many milliseconds. For a TLS listener, acceptance includes the server-side TLS handshake.
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();
socket.bindAndListen(5000, 25) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Wait for and accept the next incoming connection on the listening socket. maxWaitMs = 0 waits
// indefinitely; a positive value waits up to that many milliseconds. For a TLS listener, acceptance
// includes the server-side TLS handshake.
const connected_sock = try chilkat.Socket.init();
defer connected_sock.deinit();
socket.acceptNext(20000, connected_sock) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
std.debug.print("Accepted a connection from {s}\n", .{try connected_sock.getRemoteIpAddress(alloc)});
}