
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh) | FirstPrevNext |
|
 |
|
 |
Hello!
I found very useful this project that you developped ! Still , I have a problem in running it on a XP system .
When I try a "get" on localhost from one folder to another ,the local file is created but is empty and the following error appears: "Transfering....... Packets: 1 Bytes : 0 Done.
Error:Can't get packet."
The remote file is a txt file only 1KB long so I don't think that filesize is the problem.
When I try a "put" on localhost from one folder to another , the remote file is not created and I get the error : "Transferring....
Done. Error: recv error."
What do you suggest that I should do/configure on my computer so that I can run your application? I should mention that Firewall is turned off , so it's not preventing the file transfer.
I thank you for your help!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi!
It looks like problem on server side. According to log socket was created and write request packet was successfully sent to server. But client was get no response.
Check server IP and port number. Trace write request packet way to the server. Be sure that server really got it. Try to use another client with your TFTP server or try to use client code with another TFTP server application.
Dmitry.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for your response !
I changed the tftp server from tftpd32 to a SolarWinds and now it works fine !
Your project is awsome ! Congratulations!
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Can't reproduce this error. Sending 5000 small files to local windows TFTP server was successful and Visual Studio reported no memory leaks.
Try to call CTFTPClient::Startup(); and CTFTPClient::Cleanup(); outside the loop. May be it's some problems with winsock library.
Thanks for testing, Dmitry.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I don't see any reason for the low vote for this article. Of course, I recommend to reformat the article (remove those ") but that's not a reason to put a new writer down.
In the end, we will remember not the words of our enemies, but the silence of our friends. - Martin Luther King Jr. Ernest Laurentin
|
| Sign In·View Thread·PermaLink | 3.40/5 (3 votes) |
|
|
|
 |
|
|
 |
|
 |
If you can even call it an article, that is. It is unformatted, poorly written and it, overall, sucks.
But good job anyway.
|
| Sign In·View Thread·PermaLink | 1.83/5 (5 votes) |
|
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin