Click here to Skip to main content
6,305,776 members and growing! (17,222 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Client/Server Development     Intermediate License: The Code Project Open License (CPOL)

A simple TFTP client using C++

By dim13

A simple TFTP client implementation using C++, based on the Microsoft Winsock library.
C++ (VC6, VC7, VC7.1, VC8.0), Windows (Win2K, WinXP, Win2003, Vista, TabletPC), Win32, Visual Studio, Dev
Posted:11 Sep 2008
Views:8,059
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
23 votes for this article.
Popularity: 4.81 Rating: 3.53 out of 5
5 votes, 21.7%
1

2
1 vote, 4.3%
3
8 votes, 34.8%
4
9 votes, 39.1%
5

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


Member

Occupation: Software Developer
Location: Ukraine Ukraine

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralCan't get packet Pinmembercamelia100011:16 21 Apr '09  
GeneralRe: Can't get packet Pinmemberdim1323:50 21 Apr '09  
GeneralRe: Can't get packet Pinmembercamelia10000:31 22 Apr '09  
GeneralFailure on 4019th file transfer PinmemberBoris The Bold2:32 24 Sep '08  
AnswerRe: Failure on 4019th file transfer Pinmemberdim134:21 24 Sep '08  
GeneralWhy low vote? PinmemberErnest Laurentin12:20 11 Sep '08  
GeneralRe: Why low vote? Pinmemberbcraun5:44 13 Sep '08  
GeneralLOVE YOUR ARTICLE. PinmemberMrGoodly11:59 11 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Sep 2008
Editor: Smitha Vijayan
Copyright 2008 by dim13
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project