Click here to Skip to main content
Licence CPOL
First Posted 11 Sep 2008
Views 31,138
Downloads 1,328
Bookmarked 27 times

A simple TFTP client using C++

By | 11 Sep 2008 | Article
A simple TFTP client implementation using C++, based on the Microsoft Winsock library.

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

  1. 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);
  2. Creation

    Create the TFTP client object.

    CTFTPClient* c = new CTFTPClient();
  3. 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.

    // Sending file
    int rc = c->Put("c:\\123.txt","123.txt","127.0.0.1");
    while (rc==TFTP_RESULT_CONTINUE) rc = c->Continue();
    // Receiving file
    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.

  4. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

dim13

Software Developer

Ukraine Ukraine

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionblksize option PinmemberMember 51665618:22 3 Apr '12  
Questionwhat about server..? Pinmemberieqa44094:01 27 Dec '10  
AnswerRe: what about server..? Pinmemberdim131:29 1 Jan '11  
GeneralSmall bug in source code PinmemberIgorNazar4:10 8 Sep '10  
GeneralRe: Small bug in source code Pinmemberdim131:37 1 Jan '11  
Questionthe file size was too large? Pinmemberzhaoxueqian22:37 27 May '10  
AnswerRe: the file size was too large? Pinmemberdim1323:42 27 May '10  
GeneralRe: the file size was too large? Pinmemberzhaoxueqian0:21 28 May '10  
AnswerRe: the file size was too large? Pinmemberdicht2:01 17 Apr '12  
GeneralCan't get packet Pinmembercamelia100010:16 21 Apr '09  
GeneralRe: Can't get packet Pinmemberdim1322:50 21 Apr '09  
GeneralRe: Can't get packet Pinmembercamelia100023:31 21 Apr '09  
GeneralFailure on 4019th file transfer PinmemberBoris The Bold1:32 24 Sep '08  
I'm using your code in my application to transfer files (about 15k) to an embeded system (OS9). However during stress testing my application just shutsdown (no error message) when trying to transfer the file for the 4019th time. This is consistent. I think there may be a resource or memory leak somewhere.
 
Below is the code I'm using:
 
int icount = 0;
do
{
icount++;
CString sText;
sText.Format(L"TFTP count = %d", icount);
pMainDlg->staticStatus.SetWindowText(sText);
Logger.Event(LogError, W2A(sText));
CTFTPClient::Startup();
CTFTPClient* pTFTP = new CTFTPClient();
CString sHost;
sHost.Format(L"%d.%d.%d.%d", MA01IPAddr.S_un.S_un_b.s_b1, MA01IPAddr.S_un.S_un_b.s_b2, MA01IPAddr.S_un.S_un_b.s_b3, MA01IPAddr.S_un.S_un_b.s_b4);
 
int rc = pTFTP->Put(W2A(sMapFile), W2A(sMA01MapFile), W2A(sHost));
while (rc == TFTP_RESULT_CONTINUE) rc = pTFTP->Continue();
bTranferSucceeded = (rc == TFTP_RESULT_DONE);
 
delete pTFTP;
CTFTPClient::Cleanup();
}while (bTranferSucceeded);
 
The outer loop does no exit when the problem happens, the application just closes without the usual window application crash dialog.
AnswerRe: Failure on 4019th file transfer Pinmemberdim133:21 24 Sep '08  
QuestionWhy low vote? PinmemberErnest Laurentin11:20 11 Sep '08  
AnswerRe: Why low vote? Pinmemberbcraun4:44 13 Sep '08  
GeneralLOVE YOUR ARTICLE. PinmemberMrGoodly10:59 11 Sep '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 11 Sep 2008
Article Copyright 2008 by dim13
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid