Zig
Zig
Check FTP Connection and Login Status
See more FTP Examples
Demonstrates the connection-status properties ConnectVerified, LoginVerified, ConnectFailReason, and Greeting.
Background: When
Connect fails, these properties separate the possible causes: ConnectVerified shows whether the TCP connection was established at all, LoginVerified whether authentication then succeeded, and ConnectFailReason gives a numeric code pinpointing the failure — far more actionable than a generic error for distinguishing, say, an unreachable host from a bad password. The Greeting captures the server's opening banner, sometimes useful for identifying the server or its policies.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const ftp = try chilkat.Ftp2.init();
defer ftp.deinit();
ftp.setHostname("ftp.example.com");
ftp.setUsername("myFtpLogin");
// 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.setPassword("myPassword");
ftp.connect() catch {
// ConnectFailReason gives a numeric code describing why Connect failed.
std.debug.print("Connect failed, reason code: {d}\n", .{ftp.getConnectFailReason()});
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
};
// After a successful connect, these flags report what succeeded.
std.debug.print("TCP connection verified: {}\n", .{ftp.getConnectVerified()});
std.debug.print("Login verified: {}\n", .{ftp.getLoginVerified()});
// The Greeting property holds the server's initial greeting banner.
std.debug.print("Server greeting: {s}\n", .{try ftp.getGreeting(alloc)});
ftp.disconnect() catch {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
};
}