Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTunnel
    Integer iSshPort
    String sPassword
    Integer iListenPort
    Boolean iWaitForThreadExit
    String sTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatSshTunnel)) To hoTunnel
    If (Not(IsComObjectCreated(hoTunnel))) Begin
        Send CreateComObject of hoTunnel
    End

    //  Socket send/receive buffer sizes, in bytes.  Larger buffers can help throughput on
    //  high-latency links.
    Set ComSoRcvBuf Of hoTunnel To 4194304
    Set ComSoSndBuf Of hoTunnel To 262144

    //  Disable Nagle's algorithm to reduce latency for small, interactive packets.
    Set ComTcpNoDelay Of hoTunnel To 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.
    Set ComOutboundBindIpAddress Of hoTunnel To "192.168.1.50"
    Set ComOutboundBindPort Of hoTunnel To 0

    Set ComDestHostname Of hoTunnel To "db.internal.example.com"
    Set ComDestPort Of hoTunnel To 5432

    Move 22 To iSshPort
    Get ComConnect Of hoTunnel "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoTunnel "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move 1080 To iListenPort
    Get ComBeginAccepting Of hoTunnel iListenPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Tunnel established with custom socket options."

    Move True To iWaitForThreadExit
    Get ComCloseTunnel Of hoTunnel iWaitForThreadExit To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure