FTP(File Transfer Protocol)协议主要用来在网络上进行文件传输。FTP通讯除了有一个默认的端口21外,还有其他端口,同城两个端口同时进行数据传输。一个是默认的端口(通常为21),主要进行控制连接,即进行命令协议及服务器端响应码的传输。另一个非标准端口主要进行数据,上传下载文件等。
关于FTP协议和FTP命令的详细描述,参考《Visual C 网络通信编程实用案例精选》。
Nonzero if successful; otherwise 0. If the call fails, the Windows functionGetLastError may be called to determine the cause of the error.
Parameters
pstrDirName
A pointer to a string containing the name of the directory to create.
Remarks
Call this member function to create a directory on the connected server.
Use GetCurrentDirectory to determine the current working directory for this connection to the server. Do not assume that the remote system has connected you to the root directory.
The pstrDirName parameter can be either a partially or a fully qualified filename relative to the current directory. A backslash (/) or forward slash (/) can be used as the directory separator for either name. CreateDirectory translates the directory nameseparators to the appropriate characters before they are used.
注意:CreateDir 在FTP服务器上创建已经存在的文件夹时会 返回FALSE,而且只能创建到当前(根)目录下
Nonzero if successful; otherwise 0. If the call fails, the Win32 functionGetLastError may be called to determine the cause of the error.
Parameters
pstrDirName
A pointer to a string containing the directory to be removed.
Remarks
Call this member function to remove the specified directory from the connected server.
Use GetCurrentDirectory to determine the server’s current working directory. Do not assume that the remote system has connected you to the root directory.
The pstrDirName parameter can be either a partially or fully qualified filename relative to the current directory. A backslash (/) or forward slash (/) can be used as the directory separator for either name. RemoveDirectory translates the directory nameseparators to the appropriate characters before they are used.
注意:DeleteDir文件夹中有内容,先删除文件夹中文件,才可以删文件夹,否则返回FALSE, 删除不存在的文件夹返回FALSE 1.3 文件上传下载删除 GetFile()函数
BOOL GetFile(LPCTSTR pstrRemoteFile, LPCTSTR pstrLocalFile, BOOL bFailExists, DWORD dwAttributes, DWORD dwFlags, DWORD dwContext);
pstrFileName
需要删除的服务器上的文件名
Call this member function to delete the specified file from the connected server.
The pstrFileName parameter can be either a partially qualified filename relative to the current directory or fully qualified. A backslash (/) or forward slash (/) can be used as the directory separator for either name. The Remove function translates thedirectory name separators to the appropriate characters before they are used.
注意:Remove如果删除的文件不存在,则返回FALSE,支持相对路径 2. 测试实例 2.1 例一 连接到FTP站点
编译并且执行程序,在连接和传输的过程中,应用程序仍然可以作自己的工作,这是因为传输的过程发生在线程中。 3.封装FTPTransfer类 3.1 头文件
///////////////////////////////////////////////////////////
// FTPTransfer.h
// interface of the FTPTransfer module
// Created on: 24-July-2010
// Original author: Andrew Zhang
///////////////////////////////////////////////////////////
#ifndef _FTPTRANSFER_H_
#define _FTPTRANSFER_H_
#include <afxinet.h> // for ftp api functions
class FTPTransfer
{
public:
FTPTransfer();
~FTPTransfer();