Sample code for 30+ languages & platforms
Zig

Abort a Running SSH Tunnel Operation

See more SSH Tunnel Examples

Demonstrates the AbortCurrent property, which requests that the currently running foreground operation be aborted. It is intended to be set to true from another thread — for example, to interrupt a synchronous Connect that is waiting for an unresponsive server.

Background: A synchronous call like Connect blocks the calling thread until it finishes or times out, which is a problem when a user clicks "Cancel" or the app needs to shut down. Setting AbortCurrent from a different thread breaks the blocked operation out early, returning a failure the caller can handle. Note its scope is deliberately limited to the current foreground operation: it does not stop the background listener or terminate established tunnel clients — use StopAccepting or CloseTunnel for those.

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();

    // Demonstrates the SshTunnel.AbortCurrent property, which requests that the currently running
    // foreground operation be aborted.

    const tunnel = try chilkat.SshTunnel.init();
    defer tunnel.deinit();

    // AbortCurrent is intended to be set to true from ANOTHER thread to interrupt a synchronous
    // operation in progress -- for example, a Connect that is waiting for an unresponsive server.
    // It does not stop the background listener or terminate established background tunnel clients.
    //
    // The assignment below illustrates the property.  In a real application it would be set on a
    // separate thread while Connect blocks; if it becomes true, the blocked Connect returns with a
    // failure.
    tunnel.setAbortCurrent(false);

    tunnel.setDestHostname("db.internal.example.com");
    tunnel.setDestPort(5432);

    const ssh_port = 22;
    tunnel.connect("ssh.example.com", ssh_port) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Connected.\n", .{});

    const wait_for_thread_exit = true;
    tunnel.closeTunnel(wait_for_thread_exit) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };
}