FTP Client Example
A NetBurner embedded FTP client application that demonstrates basic FTP operations including connecting to a server, directory navigation, file listing, file creation, and file retrieval.
- Note
- For FTP and SFTP using the EFFS file systesms, refer to examples in the
\nburn\examples\EFFS folder
Overview
This application connects to an FTP server and performs the following operations:
- Establishes an FTP connection to a specified server
- Changes to a remote directory called "test1"
- Retrieves and displays the directory listing
- Creates a sample file named "WriteTest.txt" with test content
- Reads back the created file and displays its contents
Features
- FTP server connection with authentication
- Directory navigation and listing
- File upload (creation) capability
- File download and content display
- Error handling and command result validation
- Timeout handling for network operations
Hardware Requirements
- NetBurner embedded device with network connectivity
- Active network connection
Software Requirements
- NetBurner RTOS (nbrtos)
- NetBurner system libraries including:
Setup Requirements
Before running this application, ensure the following prerequisites are met:
- FTP Server Access: You need access to a functioning FTP server
- Authentication: Valid username and password for the FTP server
- Directory Structure: A directory named "test1" must exist on the FTP server
- Permissions: Write permissions must be granted to the "test1" directory
- Configuration: Update the configuration defines in main.cpp (see Configuration section)
Configuration
Before compiling and running the application, modify the following defines in main.cpp to match your FTP server setup:
#define SERVER_IP_ADDR "10.1.1.193"
#define FTP_PORT 21
#define USER "username"
#define PASS "password"
#define WRITE_TEST_FILE "WriteTest.txt"
Application Flow
- Initialization: The application initializes the NetBurner system and waits for an active network connection
- FTP Connection: Establishes a connection to the FTP server using the configured credentials
- Directory Change: Changes to the "test1" directory on the remote server
- Directory Listing: Retrieves and displays the contents of the "test1" directory
- File Creation: Creates a new file "WriteTest.txt" with sample content:
- "This is a test file"
- "This is line 2 of the test file"
- "Last Line"
- File Retrieval: Downloads and displays the contents of the newly created file
- Cleanup: Closes the FTP session and enters an infinite loop
Key Functions
ShowFileContents(int fdr)
Reads data from a file descriptor and displays it to stdout (debug serial port). Handles the data stream in 255-byte chunks with a 20-second timeout.
UserMain(void *pd)
The main application function that orchestrates all FTP operations. Includes comprehensive error handling and status reporting.
Error Handling
The application includes robust error handling for:
- FTP session initialization failures
- Directory change failures
- File listing failures
- File creation failures
- File retrieval failures
- Command result validation (expects FTP code 226 for successful operations)
Output
The application provides detailed console output including:
- Connection status messages
- Directory listing contents
- File creation confirmation
- File content display
- Error messages with specific error codes
Timeout Configuration
All FTP operations use a 5-second timeout (5 * TICKS_PER_SECOND) to prevent hanging on network issues.
Production Notes
- The application enables system diagnostics which should be removed for production code
- Consider implementing more sophisticated error recovery mechanisms for production use
- Hardcoded credentials should be replaced with secure configuration methods in production
Compilation
Compile this application using the NetBurner development tools with the standard NetBurner RTOS libraries linked.
Troubleshooting
Common issues and solutions:
- Connection Failures: Verify IP address, port, username, and password
- Directory Not Found: Ensure "test1" directory exists on the FTP server
- Permission Denied: Verify write permissions to the target directory
- Network Issues: Check network connectivity and firewall settings
- Timeout Errors: Consider increasing timeout values for slow network connections