《MFC編程:FTP多線程網絡編程實驗(2)》要點:
本文介紹了MFC編程:FTP多線程網絡編程實驗(2),希望對您有用。如果有疑問,可以聯系我們。
//定義一個結構,用于傳遞數據至控制函數中
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);
//這一段代碼是用來獲取函數調用的參數的,用法非常典型,函數調用的入口參數
//pParam是一個LPVOID類型的指針,必須將它化為FTP_INFO結構類型的指針
//變量,才能從中取出相應的數據成員
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;
}
//下載文件調用函數
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;
}
//用于上傳的線程函數
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;
}
//上傳文件調用的函數
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;
}
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/94_2.html