TFtpClient / TFtpSession
Source: source/classes/tftpcli.prg (TFtpClient), source/classes/tftpsess.prg (TFtpSession)
FiveWin provides FTP client capabilities through TFtpClient, which implements the FTP protocol for file transfer operations. It supports uploading (STOR), downloading (RETR), directory navigation (CWD, PWD), directory creation (MKD), file deletion (DELE), and binary/text transfer modes (TYPE). TFtpSession is a lightweight class used by the internal FTP server to track connected client sessions.
TFtpClient Class
DATA Members
| DATA | Type | Description |
|---|---|---|
cIPServer | Character | FTP server hostname or IP address |
nPort | Numeric | FTP server port (default 21) |
User | Character | FTP username |
Pass | Character | FTP password |
oSocket | TSocket | Control connection socket |
oDataSocket | TSocket | Data connection socket for file transfers |
nStatus | Numeric | Session state (CONNECTING, IDENTIFY1, IDENTIFY2) |
AllOk | Logical | Set to .T. after successful login |
SrvLastMsg | Character | Last server response message |
lWaiting | Logical | Busy flag during synchronous command execution |
lTimeOut | Logical | Set to .T. if a command times out (30 seconds) |
nSended | Numeric | Bytes sent counter |
bConnecting | Block | Callback during connection |
bConnected | Block | Callback when connected |
bDone | Block | Callback when operation completes |
Methods
| Method | Description |
|---|---|
New( cIPServer, nPort, User, Pass ) | Create FTP client and connect to server. nPort defaults to 21. |
PWD() | Print working directory. Returns server response. |
CWD( cDirName ) | Change working directory on the server |
MKD( cDirName ) | Create a directory on the server |
DELE( cFileName ) | Delete a file from the server |
TYPE( cType ) | Set transfer mode: "I" (binary/image) or "A" (ASCII) |
STOR( cFileName, bBlock ) | Upload a file (STORE). Uses PORT active mode. |
RETR( cFileName ) | Download a file (RETRIEVE). Returns .T. on success, .F. if file not found (550). |
PORT( oTransSocket ) | Generate PORT command string for active data connection |
QUIT() | Disconnect from the server |
DoWait() | Wait synchronously for server response (30-second timeout) |
TFtpSession Class
TFtpSession is a simple data class used internally to represent a connected FTP client session on the server side:
| DATA | Type | Description |
|---|---|---|
cUserName | Character | Authenticated username |
cPassword | Character | Authenticated password |
cCurDir | Character | Current working directory for the session |
cIP | Character | Client IP address from PORT command |
nPort | Numeric | Client port from PORT command |
oSocket | TSocket | Response socket for the session |
Example: Upload a File
#include "FiveWin.ch"
function Main()
local oFtp
oFtp := TFtpClient():New( "ftp.example.com", 21, ;
"username", "password" )
if oFtp:AllOk
oFtp:CWD( "/public_html" )
oFtp:STOR( "C:\files\index.html" )
oFtp:QUIT()
MsgInfo( "Upload complete" )
else
MsgStop( "FTP connection failed" )
endif
return nil
Example: Download a File
#include "FiveWin.ch"
function Main()
local oFtp, lResult
oFtp := TFtpClient():New( "ftp.example.com", 21, ;
"username", "password" )
if oFtp:AllOk
oFtp:CWD( "/backups" )
lResult := oFtp:RETR( "data_2026.zip" )
if lResult
MsgInfo( "Download successful" )
else
MsgAlert( "File not found on server" )
endif
oFtp:QUIT()
endif
return nil
Example: Directory Operations
#include "FiveWin.ch"
function Main()
local oFtp, cCurrentDir
oFtp := TFtpClient():New( "ftp.example.com", 21, ;
"username", "password" )
if oFtp:AllOk
// Get current directory
cCurrentDir := oFtp:PWD()
// Create a directory
oFtp:MKD( "new_folder" )
// Change into it
oFtp:CWD( "new_folder" )
// List current directory
? oFtp:PWD()
oFtp:QUIT()
endif
return nil
Notes
- TFtpClient uses active FTP mode (PORT command) for data transfers. The client opens a listening socket and sends the PORT command with IP and port information to the server.
- After calling
New(), checkAllOkto verify that login succeeded before attempting file operations. - The
DoWait()method implements synchronous command execution with a 30-second timeout. If the server does not respond in time,lTimeOutis set to.T.. - For downloads,
RETR()returns.F.if the server responds with error code 550 (file not found). - TFtpSession is designed for use with the internal FTP server class and is not typically instantiated directly by application code.