Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoFtp2.h>

BOOL success = NO;

CkoFtp2 *ftp = [[CkoFtp2 alloc] init];

ftp.Hostname = @"ftp.example.com";
ftp.Username = @"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.
ftp.Passive = YES;

//  In passive mode, optionally use EPSV before falling back to PASV.  AutoSetUseEpsv lets Chilkat
//  decide automatically.
ftp.UseEpsv = YES;
ftp.AutoSetUseEpsv = YES;

//  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.
ftp.PassiveUseHostAddr = YES;

//  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.
ftp.ActivePortRangeStart = [NSNumber numberWithInt:50000];
ftp.ActivePortRangeEnd = [NSNumber numberWithInt:50100];
ftp.ForcePortIpAddress = @"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.
ftp.Password = @"myPassword";

success = [ftp Connect];
if (success == NO) {
    NSLog(@"%@",ftp.LastErrorText);
    return;
}

NSLog(@"%@",@"Connected.");

success = [ftp Disconnect];
if (success == NO) {
    NSLog(@"%@",ftp.LastErrorText);
    return;
}