Zig
Zig
Bind and Listen for Incoming Connections
See more Socket/SSL/TLS Examples
Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.
Background. The backlog argument bounds the queue of pending connections. When
ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.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();
// Bind a TCP listener to a port and place it into listening mode. The 2nd argument is the backlog,
// the maximum number of pending connections allowed to queue.
socket.bindAndListen(5000, 25) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// After the listener is established, accept the next incoming connection into a new Socket.
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.\n", .{});
}