《MFC編程:FTP多線程網(wǎng)絡(luò)編程實驗(2)》要點(diǎn):
本文介紹了MFC編程:FTP多線程網(wǎng)絡(luò)編程實驗(2),希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:C/C++和VC / 桌面軟件開發(fā)
//定義一個結(jié)構(gòu),用于傳遞數(shù)據(jù)至控制函數(shù)中
typedef struct {
CListBox* pList; //列表框句柄
CString strFtpSite; //IP
CString strName; //姓名
CString strPwd; //密碼
}FTP_INFO;
BOOL mtDownload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName);
BOOL mtUpload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName);
UINT mtQuery(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
//這一段代碼是用來獲取函數(shù)調(diào)用的參數(shù)的,用法非常典型,函數(shù)調(diào)用的入口參數(shù)
//pParam是一個LPVOID類型的指針,必須將它化為FTP_INFO結(jié)構(gòu)類型的指針
//變量,才能從中取出相應(yīng)的數(shù)據(jù)成員
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
CInternetSession*pSession;
CFtpConnection*pConnection;
CFtpFileFind*pFileFind;
CString strFileName;
BOOL bContinue;
pConnection=NULL;
pSession = new CInternetSession;
try
{
pConnection=
pSession->GetFtpConnection(strFtpSite,strName,strPwd,21,TRUE);
}catch (CInternetSession*e) {
e->Close();
pConnection=NULL;
}
if(pConnection!=NULL)
{
pFileFind=new CFtpFileFind(pConnection);
bContinue=pFileFind->FindFile("*");
if(!bContinue)
{
pFileFind->Close();
pFileFind=NULL;
}
bContinue = pFileFind->FindNextFile();
while (bContinue)
{
strFileName=pFileFind->GetFileName();
if(pFileFind->IsDirectory()) strFileName="["+strFileName+"]";
pList->AddString(strFileName);
bContinue = pFileFind->FindNextFile();
}
if(pFileFind!=NULL)
{
pFileFind->Close();
pFileFind=NULL;
}
}
delete pFileFind;
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return 0;
}
UINT mtDownloadFile(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
int nSel=pList->GetCurSel();
CString strSourceName;
pList->GetText(nSel,strSourceName);
if(strSourceName.GetAt(0)!='[')
{
CString strDestName;
CFileDialog dlg(FALSE,"","*.*");
if(dlg.DoModal()==IDOK)
{
strDestName=dlg.GetPathName();
if(mtDownload(strFtpSite,strName,strPwd,
strSourceName,strDestName))
AfxMessageBox("下載成功!",MB_OK|MB_ICONINFORMATION);
else{
AfxMessageBox("下載失敗!",MB_OK|MB_ICONSTOP);
return FALSE;
}
}else{AfxMessageBox("請寫入文件名!",MB_OK|MB_ICONSTOP);
return FALSE;
}
}else{AfxMessageBox("不能下載目錄!\n請重選!",MB_OK|MB_ICONSTOP);
return FALSE;
}
return 0;
}
//下載文件調(diào)用函數(shù)
BOOL mtDownload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName)
{
CInternetSession*pSession;
CFtpConnection*pConnection;
pConnection=NULL;
pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
pConnection=pSession->GetFtpConnection(strFtpSite,strName,strPwd,21,TRUE);
}
catch(CInternetException*e)
{
e->Delete();
pConnection=NULL;
return FALSE;
}
if(pConnection!=NULL)
{
if(!pConnection->GetFile(strSourceName,strDestName))
{
pConnection->Close();
delete pConnection;
delete pSession;
return FALSE;
}
}
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return TRUE;
}
//用于上傳的線程函數(shù)
UINT mtUploadFile(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
CString strSourceName;
CString strDestName;
CFileDialog dlg(TRUE,"","*.*");
if(dlg.DoModal()==IDOK)
{
strSourceName=dlg.GetPathName();
strDestName=dlg.GetFileName();
if(mtUpload(strFtpSite,strName,strPwd,strSourceName,strDestName))
AfxMessageBox("上傳成功!",MB_OK|MB_ICONINFORMATION);
else
AfxMessageBox("上傳失敗!",MB_OK|MB_ICONSTOP);
}else{
AfxMessageBox("請選擇文件!",MB_OK|MB_ICONSTOP);
}return 0;
}
//上傳文件調(diào)用的函數(shù)
BOOL mtUpload(CString strFileSite,CString strName,CString strPwd,CString strSourceName,CString strDestName)
{
CInternetSession*pSession;
CFtpConnection*pConnection;
pConnection=NULL;
pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
pConnection=pSession->GetFtpConnection(strFileSite,strName,strPwd,21,TRUE);
}
catch (CInternetException*e)
{
e->Delete();
pConnection=NULL;
AfxMessageBox("上傳失敗!001",MB_OK|MB_ICONSTOP);
return FALSE;
}
if(pConnection!=NULL)
{
if (!pConnection->PutFile(strSourceName,strDestName))
{
//上傳文件錯誤
pConnection->Close();
delete pConnection;
delete pSession;
AfxMessageBox("上傳失敗!002",MB_OK|MB_ICONSTOP);
return FALSE;
}
}
//清除對象
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return TRUE;
}
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/94_2.html