Sample code for 30+ languages & platforms
Lua

Log In After ConnectOnly on an FTP Server

See more FTP Examples

Demonstrates the Chilkat Ftp2.LoginAfterConnectOnly method, which authenticates a control connection previously established by ConnectOnly. It takes no arguments, sends USER, PASS, and ACCT when required, then performs normal post-login initialization.

Background: This is the second half of the two-step connect. Beyond sending the credentials, it runs the same post-login setup Connect would — for example issuing TYPE I and any auto-negotiated features — so the session ends up in the same ready state either way. The value of the split is entirely in what you can do in the gap between the two calls.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Ftp2.LoginAfterConnectOnly method, which authenticates a control connection
-- previously established by ConnectOnly.  It takes no arguments and sends USER, PASS, and (when
-- required) ACCT, then performs normal post-login initialization.

local ftp = chilkat.Ftp2.new()

ftp.hostname = "ftp.example.com"
ftp.port = 21

-- First connect without logging in.
local success = ftp:connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

-- Set credentials (for example, after inspecting the greeting), then log in.
ftp.username = "myFtpLogin"
ftp.password = "myPassword"

success = ftp:login_after_connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

print("Logged in.")

success = ftp:disconnect()
if not success then
    print(ftp.last_error_text)
    return
end