Click here to Skip to main content
Click here to Skip to main content

A simple TFTP client using C++

By , 11 Sep 2008
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Bugrecvfrom return value is used incorrectlymemberChris Blaylock26 Jun '12 - 0:48 
Questionblksize optionmemberMember 5166563 Apr '12 - 18:22 
Questionwhat about server..?memberieqa440927 Dec '10 - 4:01 
AnswerRe: what about server..?memberdim131 Jan '11 - 1:29 
GeneralSmall bug in source codememberIgorNazar8 Sep '10 - 4:10 
GeneralRe: Small bug in source codememberdim131 Jan '11 - 1:37 
Questionthe file size was too large?memberzhaoxueqian27 May '10 - 22:37 
AnswerRe: the file size was too large?memberdim1327 May '10 - 23:42 
GeneralRe: the file size was too large?memberzhaoxueqian28 May '10 - 0:21 
AnswerRe: the file size was too large?memberdicht17 Apr '12 - 2:01 
Hi,
 
had the same Problem the fix is really simple....
 
in function ContinuePut() use the following :
 
while (true)
{
if (!Send(&p)) return TFTP_RESULT_ERROR;
 
//block number wrap around if file size > 32MB
if (m_packetnum>65535)
m_packetnum=0;
 
if (WaitACK(m_packetnum)) break;
retrycount++;
if (retrycount>TFTP_RETRY)
{
Error("No answer.");
return TFTP_RESULT_ERROR;
}
}
 

regards
dicht
GeneralCan't get packetmembercamelia100021 Apr '09 - 10:16 
GeneralRe: Can't get packetmemberdim1321 Apr '09 - 22:50 
GeneralRe: Can't get packetmembercamelia100021 Apr '09 - 23:31 
GeneralFailure on 4019th file transfermemberBoris The Bold24 Sep '08 - 1:32 
AnswerRe: Failure on 4019th file transfermemberdim1324 Sep '08 - 3:21 
QuestionWhy low vote?memberErnest Laurentin11 Sep '08 - 11:20 
AnswerRe: Why low vote?memberbcraun13 Sep '08 - 4:44 
GeneralLOVE YOUR ARTICLE.memberMrGoodly11 Sep '08 - 10:59 

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

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