Sample code for 30+ languages & platforms
Zig

SSH Tunnel TCP Socket Options

See more SSH Tunnel Examples

Demonstrates tuning the underlying TCP socket with the SoRcvBuf, SoSndBuf, TcpNoDelay, OutboundBindIpAddress, and OutboundBindPort properties. These normally should be left at their defaults.

Background: These are low-level knobs for specific situations. Larger send/receive buffers can improve throughput on high-bandwidth, high-latency ("long fat") links; TcpNoDelay disables Nagle's algorithm to cut latency for small interactive packets at the cost of efficiency; and binding the outbound socket to a specific local address or port is occasionally required by firewall rules or multi-homed hosts. Change them only with a concrete reason, since the defaults are tuned for the common case.

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 tuning the underlying TCP socket with the SshTunnel properties SoRcvBuf, SoSndBuf,
    // TcpNoDelay, OutboundBindIpAddress, and OutboundBindPort.
    //
    // These normally should be left at their defaults; adjust them only for specific performance or
    // networking requirements.

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

    // Socket send/receive buffer sizes, in bytes.  Larger buffers can help throughput on
    // high-latency links.
    tunnel.setSoRcvBuf(4194304);
    tunnel.setSoSndBuf(262144);

    // Disable Nagle's algorithm to reduce latency for small, interactive packets.
    tunnel.setTcpNoDelay(true);

    // Bind the outbound socket to a specific local address/port before connecting.  0 (the default)
    // lets the OS choose the local port; an empty address lets the OS choose the interface.
    tunnel.setOutboundBindIpAddress("192.168.1.50");
    tunnel.setOutboundBindPort(0);

    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;
    };

    // Normally you would not hard-code the password in source.  You should instead obtain it
    // from an interactive prompt, environment variable, or a secrets vault.
    const password = "mySshPassword";

    tunnel.authenticatePw("mySshLogin", password) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    const listen_port = 1080;
    tunnel.beginAccepting(listen_port) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Tunnel established with custom socket options.\n", .{});

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