
Introduction
TFTP stands for Trivial File Transfer Protocol. It is still used today for transferring new firmware to routers and other embedded devices, and also for booting network terminals. For a detailed description, see RFC 1350.
Looking for a simple C++ implementation of this protocol, I couldn't find one. So, I had to write it. Hope this code will save some time for someone.
Using the Code
- Initialization
The client uses the Winsock library, so you have to initialize it manually before using the code, or use the static function CTFTPClient::Startup.
CTFTPClient::Startup();
or
WSADATA w;
WSAStartup(0x0101, &w);
- Creation
Create the TFTP client object.
CTFTPClient* c = new CTFTPClient();
- Transfer
Use the Get and Put functions to start the file transfer. When successful, it returns the TFTP_RESULT_CONTINUE value. Then, you have to call the Continue function to transfer another packet, till it returns TFTP_RESULT_DONE. Transferring the process can be controlled by reading the number of bytes and packets using the functions PacketsCount and BytesCount.
int rc = c->Put("c:\\123.txt","123.txt","127.0.0.1");
while (rc==TFTP_RESULT_CONTINUE) rc = c->Continue();
rc = c->Get("c:\\321.txt","123.txt","127.0.0.1");
while (rc==TFTP_RESULT_CONTINUE) rc = c->Continue();
If TFTP_RESULT_ERROR was returned by Get, Put, or Continue, use LastError to see what happens. See the demo project main function for a more detailed example.
- Cleanup
Delete the TFTP client object and free the Winsock library.
delete c;
CTFTPClient::Cleanup();
or
delete c;
WSACleanup();
Notes
Some protocol parameters were hard-coded. See the next macro in the tftpclient.h file.
TFTP_TIMEOUT - timeout waiting packet.
TFTP_RETRY - how many times resend packet, when server not responding.
TFTP_DEFAULT_MODE - TFTP transfer mode.
History
- 11.09.08 - Initial release.