Sample code for 30+ languages & platforms
DataFlex

FTP Active vs. Passive Data Connections

See more FTP Examples

Demonstrates the data-connection-mode properties Passive, UseEpsv, AutoSetUseEpsv, PassiveUseHostAddr, and the active-mode properties ActivePortRangeStart, ActivePortRangeEnd, and ForcePortIpAddress.

Background: FTP's split into a control connection and separate data connections is the source of most of its firewall trouble. In passive mode (the default, and usually correct) the client opens the data connection, which works through client-side NAT. In active mode the server connects back, requiring the client to be reachable — hence the port-range and advertised-IP settings for opening firewall holes. EPSV is the IPv6-friendly modern form of passive with automatic PASV fallback, and PassiveUseHostAddr ignores a private IP a misconfigured server may report.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "myFtpLogin"

    //  Passive mode (the default) has the client connect to the server for each data transfer, which
    //  works through most client-side firewalls.  Active mode has the server connect back to the
    //  client.
    Set ComPassive Of hoFtp To True

    //  In passive mode, optionally use EPSV before falling back to PASV.  AutoSetUseEpsv lets Chilkat
    //  decide automatically.
    Set ComUseEpsv Of hoFtp To True
    Set ComAutoSetUseEpsv Of hoFtp To True

    //  PassiveUseHostAddr (default True) uses the control-connection host address rather than the
    //  address returned by PASV, which avoids problems when a server reports a private IP.
    Set ComPassiveUseHostAddr Of hoFtp To True

    //  The following apply only in ACTIVE mode (Passive = False): restrict the local port range the
    //  client listens on, and set the IP advertised to the server.
    Set ComActivePortRangeStart Of hoFtp To 50000
    Set ComActivePortRangeEnd Of hoFtp To 50100
    Set ComForcePortIpAddress Of hoFtp To "203.0.113.5"

    //  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.
    Set ComPassword Of hoFtp To "myPassword"

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connected."

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure