home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Putting the FTP Protocol to Work

FTP stands for File Transfer Protocol, and as you can imagine, there are many file-handling aspects here. This protocol is supported by the CFtpConnection class, whose file-handling functions appear in Table 8.2.


Table 8.2 CFtpConnection Class Functions

Function Does this
CFtpConnection Constructs a CFtpConnection object.
Close Closes the connection to the server.
CreateDirectory Creates a directory on the server.
GetCurrentDirectory Gets the current directory for this connection.
GetCurrentDirectoryAsURL Gets the current directory for this connection as a URL.
GetFile Gets a file from the connected server
OpenFile Opens a file on the connected server.
PutFile Places a file on the server.
Remove Removes a file from the server.
RemoveDirectory Removes the specified directory from the server.
Rename Renames a file on the server.
SetCurrentDirectory Sets the current FTP directory.


The Internet Transfer Control

In addition to using classes like CHttpConnection, CFtpConnection, or CInternetFile, you can also use the Microsoft Internet control, which comes with Visual C++. This control supports some of the more common HTTP and FTP actions and includes an OpenUrl() method.


Using these functions, you can perform the standard FTP operations, from uploading and downloading files to creating new directories. Next, we put the FTP protocol to work and download a file from the Microsoft FTP site.

Creating the FTP Program

In the next example, we use the CFtpConnection class to support a typical FTP action—downloading a file. In this case, we download a file that Microsoft keeps in its FTP main directory, named dirmap.txt. This file holds an outline of the Microsoft FTP site, and it’s one that Microsoft always keeps in its FTP directory.

Create a new MDI project now named FTP and, as we did in the last example, base the view class on the CEditView class in Step 6 of AppWizard. Next, add one new item to the File menu: Download file. When the user selects this item, we open an FTP connection to dirmap.txt at ftp.microsoft.com and download it, then display it.

We start by including the afxinet.h header.

// FTPView.cpp : implementation of the CFTPView class
//

#include “stdafx.h”
#include “FTP.h”

#include “FTPDoc.h”
#include “FTPView.h”
#include <afxinet.h>                                                 ⇐
    .
    .
    .

Next, we add code to the Download file menu item function OnFiledownloadfile() to create a new Internet session.

void CFTPView::OnFileDownloadfile()
 {
    CInternetSession* Session = new CInternetSession();               ⇐
        .
        .
        .

Creating an FTP Connection

Now we create a CFtpConnection object using the CInternetSession class’s GetFtpConnection() function, connecting to ftp.microsoft.com.

void CFTPView::OnFileDownloadfile()
 {
    CInternetSession* Session = new CInternetSession();

    CFtpConnection* FTPConnection =
Session->GetFtpConnection(“<ftp.microsoft.com>”);                      ⇐

    if(FTPConnection == NULL)                                          ⇐
        return;                                                        ⇐
            .
            .
            .

We’re ready to download the dirmap.txt file from Microsoft.

Downloading a File

We download dirmap.txt, making a copy of that file locally, with the CFTP-Connection class’s GetFile() function. We pass that function the name of the file to get, the local name we want to give it, and a parameter we set to FALSE to overwrite a local copy of the file if it already exists.

void CFTPView::OnFileDownloadfile()
 {
    CInternetSession* Session = new CInternetSession();

    CFtpConnection* FTPConnection =
Session->GetFtpConnection(“<ftp.microsoft.com>”);

    if(FTPConnection == NULL)
        return;

    FTPConnection->GetFile(“dirmap.txt”, “dirmap.txt“, FALSE);         ⇐
        .
        .
        .

At this point, then, we have downloaded dirmap.txt onto disk. To display that file, we read it back in.

Reading the File in from Disk with CFile

To read a file from disk, we use the MFC CFile class. First, we set up a buffer for that file’s data.

void CFTPView::OnFileDownloadfile()
 {
    #define BufferLength 2048                                          ⇐

    CInternetSession* Session = new CInternetSession();

    CFtpConnection* FTPConnection =
Session->GetFtpConnection(“<ftp.microsoft.com>”);

    if(FTPConnection == NULL)
        return;

    FTPConnection->GetFile(“dirmap.txt”, “dirmap.txt“, FALSE);

    char FTPData[BufferLength];                                        ⇐
        .
        .
        .

Next, we open the file dirmap.txt by passing that filename to the CFile constructor and indicating that we’re opening this file for reading. Next, we read 2K of data in from the file on disk into the buffer using the CFile Read() function.

void CFTPView::OnFileDownloadfile()
{
    #define BufferLength 2048

    CInternetSession* Session = new CInternetSession();

    CFtpConnection* FTPConnection =
Session->GetFtpConnection(“<ftp.microsoft.com>”);

    if(FTPConnection == NULL)
         return;

    FTPConnection->GetFile(“dirmap.txt”, “dirmap.txt”, FALSE);

    char FTPData[BufferLength];
    CFile file(“dirmap.txt”, CFile::modeRead);                         ⇐
    file.Read(FTPData, BufferLength);                                  ⇐
        .
        .
        .


Using CFile instead of Serialization

If you’re only interested in standard file handling, and don’t need all the attributes of serialization, use the CFile class—the member functions are simple, direct, and work well.



Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.