- 中查找“CMultiFTP类源代码”更多相关内容
- 中查找“CMultiFTP类源代码”更多相关内容
- ·上一篇文章:“变速齿轮”研究手记
- ·下一篇文章:实现FTP多线程下载
CMultiFTP类源代码
为了让大家更好的理解我发表的<<实现FTP多线程下载>>,我把我自己编写的CMultiFTP类贴上来。这段代码我不是很满意(以前写的,请写的较仓促),
但主要供大家参考,更好得理解多线程下载的实现。
// MultiFTP1.cpp: implementation of the CMultiFTP class.
//
//////////////////////////////////////////////////////////////////////
/*
#include "stdafx.h"
#include "MultiFTP.h"
#include "MultiFTP1.h"
#include <afxmt.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define BUFFERSIZE 5000
CCriticalSection gcs_multiftp;
struct stThreadParam
{
CString szLocalname;
CString szRemotename;
CString szHost;
CString szUsername,szPassword;
int ID;
int nPort;
CMultiFTP *pFtp;
UINT uStart,uLength;
};
UINT DownloadThread(LPVOID pVoid)
{
stThreadParam *pvar=(stThreadParam *)pVoid;
int ID=pvar->ID ,nPort=pvar->nPort;
CString szLocalname=pvar->szLocalname,szRemotename=pvar->szRemotename;
CString szHost=pvar->szHost ;
CString szUsername=pvar->szUsername ,szPassword=pvar->szPassword ;
CMultiFTP *pFtp=pvar->pFtp;
UINT uStart=pvar->uStart ,uLength=pvar->uLength;
delete pvar;
file://init over
CString szMsg;
CFTPGetFile m_getfile(pFtp);
CFile m_file;
char *pBuffer;
if(!m_getfile.Connect(szHost,nPort,szUsername,szPassword))
{
szMsg.Format("connect to data port failID:%dError:%s",ID,m_getfile.szMsg);
AfxMessageBox(szMsg);
return 0;
}
m_file.Open(szLocalname,CFile::modeWriteCFile::modeCreate);
pBuffer=(char *)VirtualAlloc(NULL,uLength,MEM_RESERVEMEM_COMMIT,PAGE_READWRITE);
ASSERT(pBuffer);
if(!m_getfile.OpenFile(szRemotename,szLocalname,uStart,uLength))
{
m_file.Close();
DeleteFile(szLocalname);
VirtualFree(pBuffer,0,MEM_RELEASE);
szMsg.Format("open file failID:%dError:%s",ID,m_getfile.szMsg);
AfxMessageBox(szMsg);
return 0;
}
file://get file contents
for(UINT uRead;uLength;uLength-=uRead)
{
uRead=m_getfile.Read(pBuffer,uLength);
m_file.Write(pBuffer,uRead);
}
m_file.Close();
VirtualFree(pBuffer,0,MEM_RELEASE);
gcs_multiftp.Lock();
pFtp->OnThreadOver(ID,0,"ok");
gcs_multiftp.Unlock();
return 0;
}
CMultiFTP::CMultiFTP()
{
skClient.Create();
nThreads=1;
blContinue=FALSE;
}
CMultiFTP::~CMultiFTP()
{
skClient.ShutDown(2);
skClient.Close();
}
int CMultiFTP::GetThreads()
{
return nThreads;
}
int CMultiFTP::SetThreads(int n)
{
if(blContinue)
nThreads=n;
return nThreads;
}
BOOL CMultiFTP::Connect(CString szHost, UINT Port, CString szUser, CString szPass)
{
ASSERT(skClient.m_hSocket);
szHostname=szHost;
nPort=Port;
szUsername=szUser;
szPassword=szPass;
szMsg=="";
if(!skClient.Connect(szHostname,nPort))
{
return FALSE;
}
GetMsg();
CString szCommand;
szCommand.Format("user %s",szUsername);
DoCommand(szCommand);
szCommand.Format("pass %s",szPassword);
if(DoCommand(szCommand)!=FTP_LOGOK)
return FALSE;
if(szMsg[3]=='-')
GetMsg();
if(DoCommand("rest 100")==FTP_RESTOK)
{
blContinue=TRUE;
}
else
{
nThreads=1;
blContinue=FALSE;
}
DoCommand("type a");
return TRUE;
}
void CMultiFTP::GetMsg()
{
szMsg="";
char chMsg[256];
int nRecv;
nRecv=skClient.Receive(chMsg,255);
if(nRecv==SOCKET_ERROR nRecv==0) return;
chMsg[nRecv]='