Wiki

 
Main Page | About | Help | FAQ | Special pages | Log in

Tutorialftpclient

[edit] FTPClient

In this tutorial we will show you how to write a simple FTP client that is capable of downloading a file to our local hard drive. And yes, this is a tutorial for beginners biggrin.

The following libraries are needed:

   * PocoFoundation
   * PocoNet 

[edit] Network Streams

Ideally one wants to work with network addresses as easily as with local files. For our FTP client this means that we want to create an output stream to a local file, and an input stream to the remote FTP file.

Creating the output stream is simple:

 std::ofstream localFile(fileName, std::ios_base::out | std::ios_base::binary);
 
 if (!localFile)
 {
      std::cerr << "Failed to open local file for writing: " << fileName << std::flush;
 
    return;
 }


Creating the input stream is equally simple, we use the URIStreamOpener class to open the FTP URI. but before we can do so, we have to tell our application that we want to use FTP streams, the following line should be at the very beginning of every application working with FTP stream:

Poco::Net::FTPStreamFactory::registerFactory();

Once we have done that we can create the FTP input stream:

Poco::URI uri(ftpFile);

std::auto_ptr<std::istream> ptrFtpStream(Poco::Net::URIStreamOpener::defaultOpener().open(uri));

Now that we have created the input and the output stream, we only have to read from the input and write to the output. We could do that manually, but we won't. The StreamCopier class from PocoFoundation? does that for us:

Poco::StreamCopier::copyStream(*ptrFtpStream.get(), localFile);

Putting it all together:

#include "Poco/URIStreamOpener.h"
#include "Poco/StreamCopier.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/FTPStreamFactory.h"
#include <memory>
#include <iostream>
#include <fstream>

int main(int argc, char** argv)
{
	Poco::Net::FTPStreamFactory::registerFactory();
	
	if (argc != 3)
	{
		std::cerr << "Missing Params: ftp-location localfile"<< std::endl;
		return 1;
	}

	try
	{
		std::ofstream localFile(argv[2], std::ios_base::out | std::ios_base::binary);
		if (!localFile)
		{
			std::cerr << "Failed to open local file for writing: " << argv[2]<< std::flush;
			return 1;
		}
		Poco::URI uri(argv[1]);
		std::auto_ptr<std::istream> ptrFtpStream(Poco::URIStreamOpener::defaultOpener().open(uri));
		Poco::StreamCopier::copyStream(*ptrFtpStream.get(), localFile);
	}
	catch (Poco::Exception& exc)
	{
		std::cerr << exc.displayText() << std::endl;
		return 1;
	}
		
	return 0;
}

That's it! We are done with our FTP client! And if you are currently thinking about adding HTTP support to that application as well, I have to disappoint you. It will just keep you occupied for, 30 seconds. Simply add the following:

[...]
#include "Poco/Net/HTTPStreamFactory.h"
[...]
Poco::Net::HTTPStreamFactory::registerFactory();
[...]

What? Still not satisfied? Now you are asking for HTTPS too? This might come as a surprise but if you add this:

[...]
#include "Poco/Net/HTTPSStreamFactory.h"
[...]
Poco::Net::HTTPSStreamFactory::registerFactory();
[...]

then HTTPS will work, too.

Retrieved from "http://pocoproject.org/wiki/index.php/Tutorialftpclient"

This page has been accessed 3,188 times. This page was last modified on 19 February 2010, at 20:59.


 
Find

Browse
Main Page
Community portal
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Log in / create account
Special pages
New pages
File list
Statistics
Bug reports
More...