////////////////////////////////////////////////////////////////////////////////
//
// The official specification of the File Transfer Protocol (FTP) is the RFC 959.
// Most of the documentation are taken from this RFC.
// This is an implementation of an simple ftp client. I have tried to implement
// platform independent. For the communication i used the classes CBlockingSocket,
// CSockAddr, ... from David J. Kruglinski (Inside Visual C++). These classes are
// only small wrappers for the sockets-API.
// Further I used a smart pointer-implementation from Scott Meyers (Effective C++,
// More Effective C++, Effective STL).
// The implementation of the logon-sequence (with firewall support) was published
// in an article on Codeguru by Phil Anderson.
// The code for the parsing of the different FTP LIST responses is taken from
// D. J. Bernstein (http://cr.yp.to/ftpparse.html). I only wrapped the c-code in
// a class.
// I haven't tested the code on other platforms, but i think with little
// modifications it would compile.
//
// Copyright (c) 2004 Thomas Oswald
//
// Permission to copy, use, sell and distribute this software is granted
// provided this copyright notice appears in all copies.
// Permission to modify the code and to distribute modified code is granted
// provided this copyright notice appears in all copies, and a notice
// that the code was modified is included with the copyright notice.
//
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
//
// History:
// v1.1 released 2005-12-04
// - Bug in OpenPassiveDataConnection removed: SendCommand was called before data connection was established.
// - Bugs in GetSingleResponseLine removed:
// * Infinite loop if response line doesn't end with CRLF.
// * Return value of std:string->find must be checked against npos.
// - Now runs in unicode.
// - Streams removed.
// - Explicit detaching of observers are not necessary anymore.
// - ExecuteDatachannelCommand now accepts an ITransferNotification object.
// Through this concept there is no need to write the received files to a file.
// For example the bytes can be written only in memory or an other tcp stream.
// - Added an interface for the blocking socket (IBlockingSocket).
// Therefore it is possible to exchange the socket implementation, e.g. for
// writing unit tests (by simulating an specific scenario of an ftp communication).
// - Replaced the magic numbers concerning the reply codes by a class.
// v1.0 released 2004-10-25
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FTPclient.h"
#include <limits>
#include <algorithm>
#include "FTPListParse.h"
#include "FTPFileState.h"
#undef max
#ifdef __AFX_H__ // MFC only
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
using namespace nsHelper;
namespace nsFTP
{
class CMakeString
{
public:
CMakeString& operator<<(DWORD dwNum)
{
DWORD dwTemp = dwNum;
int iCnt=1; // name lookup of 'iCnt' changed for new ISO 'for' scoping
for( ; (dwTemp/=10) != 0; iCnt++ )
;
m_str.resize(m_str.size() + iCnt);
tsprintf(&(*m_str.begin()), _T("%s%u"), m_str.c_str(), dwNum);
return *this;
}
CMakeString& operator<<(const tstring& strAdd)
{
m_str += strAdd;
return *this;
}
operator tstring() const { return m_str; }
private:
tstring m_str;
};
tstring& ReplaceStr(tstring& strTarget, const tstring& strToReplace, const tstring& strReplacement)
{
size_t pos = strTarget.find(strToReplace);
while( pos != tstring::npos )
{
strTarget.replace(pos, strToReplace.length(), strReplacement);
pos = strTarget.find(strToReplace, pos+1);
}
return strTarget;
}
class CFile : public ITransferNotification
{
FILE* m_pFile;
public:
enum T_enOrigin { orBegin=SEEK_SET, orEnd=SEEK_END, orCurrent=SEEK_CUR };
CFile() : m_pFile(NULL) {}
~CFile()
{
Close();
}
bool Open(const tstring& strFileName, const tstring& strMode)
{
m_pFile = fopen(CCnv::ConvertToString(strFileName).c_str(),
CCnv::ConvertToString(strMode).c_str());
return m_pFile!=NULL;
}
bool Close()
{
FILE* pFile = m_pFile;
m_pFile = NULL;
return pFile && fclose(pFile)==0;
}
bool Seek(long lOffset, T_enOrigin enOrigin)
{
return m_pFile && fseek(m_pFile, lOffset, enOrigin)==0;
}
long Tell()
{
if( !m_pFile )
return -1L;
return ftell(m_pFile);
}
size_t Write(const void* pBuffer, size_t itemSize, size_t itemCount)
{
if( !m_pFile )
return 0;
return fwrite(pBuffer, itemSize, itemCount, m_pFile);
}
size_t Read(void* pBuffer, size_t itemSize, size_t itemCount)
{
if( !m_pFile )
return 0;
return fread(pBuffer, itemSize, itemCount, m_pFile);
}
virtual void OnBytesReceived(const TByteVector& vBuffer, long lReceivedBytes)
{
Write(&(*vBuffer.begin()), sizeof(TByteVector::value_type), lReceivedBytes);
}
virtual void OnPreBytesSend(TByteVector& vBuffer, size_t& bytesToSend)
{
bytesToSend = Read(&(*vBuffer.begin()), sizeof(TByteVector::value_type), vBuffer.size());
}
};
class COutputStream : public ITransferNotification
{
const tstring mc_strEolCharacterSequence;
tstring m_vBuffer;
tstring::iterator m_itCurrentPos;
public:
COutputStream(const tstring& strEolCharacterSequence) :
mc_strEolCharacterSequence(strEolCharacterSequence),
m_itCurrentPos(m_vBuffer.end()) {}
void SetStartPosition()
{
m_itCurrentPos = m_vBuffer.begin();
}
bool GetNextLine(tstring& strLine)// const
{
tstring::iterator it = std::search(m_itCurrentPos, m_vBuffer.end(), mc_strEolCharacterSequence.begin(), mc_strEolCharacterSequence.end());
if( it == m_vBuffer.end() )
return false;
strLine.assign(m_itCurrentPos, it);
m_itCurrentPos = it + mc_strEolCharacterSequence.size();
return true;
}
virtual void OnBytesReceived(const TByteVector& vBuffer, long lReceivedBytes)
{
std::copy(vBuffer.begin(), vBuffer.begin()+lReceivedBytes, std::back_inserter(m_vBuffer));
}
};
};
using namespace nsFTP;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/// constructor
/// @param[in] pSocket Instance of socket class which will be used for
/// communication with the ftp server.
/// CFTPClient class takes ownership of this instance.
/// It will be deleted on destruction of this object.
/// If this pointer is NULL, the CBlockingSocket implementation
/// will be used.
/// This gives the ability to set an other socket class.
/// For example a socket class can be implemented which simulates
/// a ftp server (for unit testing).
/// @param[in] uiTimeout Timeout used for socket operation.
/// @param[in] uiBufferSize Size of the buffer used for sending and receiving
/// da
没有合适的资源?快使用搜索试试~ 我知道了~
FTP Client Class
共43个文件
h:18个
cpp:15个
avi:4个
4星 · 超过85%的资源 需积分: 9 23 下载量 65 浏览量
2011-03-17
12:52:08
上传
评论
收藏 144KB ZIP 举报
温馨提示
很好用的FTP封装类 not based on MFC-sockets not using other MFC-stuffs like CSring(uses STL) supports Firewalls supports resuming can be easily extended
资源推荐
资源详情
资源评论
收起资源包目录
FTPClient_demo.zip (43个子文件)
FTPClient_demo
FTPexampleDlg.cpp 3KB
FTPexample.h 228B
FTPClient.h 36KB
FTPexample.cpp 810B
FTPFileState.cpp 4KB
FTPFileState.h 9KB
stdafx.h 664B
ShellImageList.cpp 4KB
BlockingSocket.cpp 11KB
FTPClient.cpp 64KB
FTPLogonInfoDlg.h 1KB
FTPFileTreeCtrl.cpp 6KB
rc_smart_ptr.h 3KB
FTPFileTreeCtrl.h 2KB
FTPLogonInfoDlg.cpp 5KB
FTPProgressDlg.h 2KB
stdafx.cpp 21B
FTPProtocolOutput.cpp 2KB
FTPProtocolOutput.h 867B
FTPexample.sln 909B
FTPListParse.h 2KB
FTPProgressDlg.cpp 5KB
FTPListParse.cpp 18KB
FTPDataTypes.h 16KB
FTPexample.vcproj 6KB
FTPBrowseForFileAndFolder.h 862B
Resource.h 2KB
Release
FTPexample.exe 144KB
FTPDataTypes.cpp 5KB
FTPFirewallTypeComboBox.h 664B
FTPexample.rc 9KB
res
Upload.avi 14KB
Search.avi 20KB
Download.avi 14KB
Filecopy.avi 9KB
FTPexample.manifest 721B
FTPexample.ico 21KB
Definements.h 6KB
ShellImageList.h 2KB
FTPFirewallTypeComboBox.cpp 2KB
FTPexampleDlg.h 917B
BlockingSocket.h 7KB
FTPBrowseForFileAndFolder.cpp 1KB
共 43 条
- 1
资源评论
- yhkang2013-10-20小型的FTP 客户端 好用
- qingsha1102014-06-09可以使用, 但是不能上传,下载大文件。
- openfine2013-11-15小型的FTP 客户端 好用
- myfys2013-04-15没有说明,慢慢看
- hh_1015_csdn2015-01-27可以编译通过,可以正常使用,不过我只需要其中一部分
jing0611
- 粉丝: 40
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功