Qt SCXML FTP Client Example¶
Implements a simple FTP client using a state machine.
FTP Client uses Qt SCXML to implement a FTP client that can communicate with a FTP service by sending FTP control messages translated from state machine events and by translating server replies into state machine events. The data received from the FTP server is printed on the console.
RFC 959 specifies state charts for the command handling of the FTP client. They can be easily translated into SCXML to benefit from SCXML nested states. Connections between the client and server and data transfer are implemented by using C++. In addition, Qt signals and slots are used.
The state machine has the following states:
I as the initial state.
B for sending commands.
S for success.
F for failure.
W for waiting for a reply.
P for supplying a password upon server request.
The state machine is specified in the simpleftp.scxml file and compiled into the FtpClient
class that implements the logic of the FTP protocol. It reacts to user input and to replies from the control channel by changing states and sending external events. In addition, we implement a FtpControlChannel
class and a FtpDataChannel
class that handle TCP sockets and servers and convert line endings.
Running the Example¶
To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Compiling the State Machine¶
We link against the Qt SCXML module by adding the following line to the project build files.
With qmake, we add the following to ftpclient.pro
We then specify the state machine to compile:
With CMake, we add the following to CMakeLists.txt
We then specify the state machine to compile:
The Qt SCXML Compiler, qscxmlc
, is run automatically to generate simpleftp.h and simpleftp.cpp, and to add them appropriately to the project as headers and sources.
Instantiating the State Machine¶
We instantiate the generated FtpClient
class, as well as the FtpDataChannel
and FtpControlChannel
classes in the main.cpp file:
...
...
...
Communicating with an FTP Server¶
We print all data retrieved from the server on the console:
We translate server replies into state machine events:
We translate commands from the state machine into FTP control messages:
We send commands to log into the FTP server as an anonymous user, to announce a port for the data connection, and to retrieve a file:
We specify that the FTP client should send the next command when entering the B state:
We specify that the FTP client should send an empty string as a password if the server asks for one:
Finally, we connect to the FTP server specified as the first argument of the method and retrieve the file specified as the second argument:
For example, the following invocation prints the specified file from the specified server: ftpclient <server> <file>
.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.