Zig
Zig
Poll a Socket for Available Data
See more Socket/SSL/TLS Examples
Demonstrates Socket.PollDataAvailable, which performs a nonblocking check for readable activity and returns whether data or another read-ready condition is present.
Background. This lets an application check for incoming data without blocking, for example in an event loop that services multiple tasks.
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();
// Connect to the server using TLS.
const b_tls = true;
const max_wait_ms = 5000;
socket.connect("example.com", 5000, b_tls, max_wait_ms) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Perform a nonblocking check for readable data. Returns true when data or another read-ready
// condition is present, and false when nothing is immediately available.
const b_available = socket.pollDataAvailable();
if (!b_available) {
std.debug.print("No data is immediately available.\n", .{});
} else {
std.debug.print("Data is available to read.\n", .{});
}
}