A simple FTP client class

Environment: VC5

Anyone who has used the WinInet FTP functions has probably noticed that there are a few handy features that are lacking. In particular, I wanted some code that would:

  • Support the same firewalls as WS-FTP
  • Allow me to execute arbitrary commands on the FTP server
  • Add around 10K to my app install rather than WinInet's 1Mb #-)
The whole thing is implemented as a single class and there is nothing clever about it at all, eg all socket connections are in synchronous mode. This does mean that if you want to go off and do other things whilst the file transfer is in progress you'll have to run it in it's own thread.

To use:
If you created your app without checking the "Use Windows Sockets" checkbox in AppWizard, you'll need to add the following bit of code to you app's InitInstance()


if(!AfxSocketInit())
{
	AfxMessageBox("Could not initialize Windows Sockets!");
	return FALSE;
}
You'll also need to insert the files CFTPclient.cpp and CFTPclient.h into your project. There are a few error messages in the demo project's string table that you'll need to copy across also. That done:
  1. Create an object of CFTPclient.
  2. Use LogOnToServer() to connect to the server. Any arguments not used (e.g. if you're not using a firewall), pass an empty string or zero for numeric args. You must pass a server port number, use the FTP default of 21 if you don't know what it is.
  3. Use MoveFile() to upload/download a file, 1st arg is local file path, 2nd arg is remote file path, 3rd arg is TRUE for a PASV connection (required by some firewalls), FALSE otherwise, 4th arg is TRUE to upload, FALSE to download file. MoveFile only works in synchronous mode (ie the function will not return 'till the transfer is finished). File transfers are always of type BINARY.
  4. You can use FTPcommand() to execute FTP commands. Note that this function will return FALSE unless the server response is a 200 series code. This should work fine for most commands, otherwise you can use WriteStr() and ReadStr() to send commands & interpret the response yourself. Use LogOffServer() to disconnect when done. Note: The required FTP server commands may have different syntax to a command line FTP client you may be used to. Check out RFC 959 for more nfo on what the available commands are.
Unfortunately I don't have access to all the different types of firewalls supported. So far I've only tested it using no firewall and a "USER with no logon" type firewall (which worked OK first time so I guess some of the others might work too). #;-)

Useage example:

CFTPclient ftp;

// connect to FTP server
if(!ftp.LogOnToServer("ftp.server.com",21,"mylogon","mypass","","","","",0,0))	
{
	MessageBox(ftp.m_retmsg);
	return;
}

// change directory on FTP server
if(!ftp.FTPcommand("CWD /home/images"))
{
	MessageBox(ftp.m_retmsg);
	ftp.LogOffServer();
	return;
}

// upload a file
ftp.MoveFile("myfile.gif","myfile.gif",FALSE,TRUE);

// show the result of the operation
MessageBox(ftp.m_retmsg);

// disconnect from server
ftp.LogOffServer();


Download demo project - [38KB]



Comments

  • IT & MBA

    Posted by chabdel on 11/05/2014 07:02am

    Many thanks for this example.

    Reply
  • Great post

    Posted by Mirtha on 09/09/2014 09:08am

    brilliant

    Reply
  • cheap uggs

    Posted by WokHoossyfeer on 11/11/2012 01:57pm

    ugg boots outlet onlinet bootsoutletus.weebly.com bootsoutletonlinestore.weebly.com

    Reply
  • Leather Ugg Boots

    Posted by WokHoossyfeer on 11/11/2012 01:57pm

    bootsoutletus.weebly.com bootsoutletonlinestore.weebly.com UGG Outlet Store

    Reply
  • http://www.officialravensfanstore.com/ed-reed-nike-jersey-c-8_14.html tuctanneguina

    Posted by bereBlepbal on 11/07/2012 03:19am

    kneessystek Terrell Suggs Jersey DeMarcus Ware Youth Jersey Ray Rice White Jersey

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: August 27, 2015 With the cloud enabling companies to spin up servers and stand up data stores more quickly, mobile apps can be created faster, reducing the time-to-value. But three major obstacles stand in the way for many organizations: The backlog of app project requests confronting every enterprise regardless of their internal app development capabilities Finding and employing better, faster tools to speed and simplify the process of developing those apps. The emergence of …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date