⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 学习网络编程的初学者必看的.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
	CString str=(LPCTSTR)wParam;
	m_ctLocDir.SetWindowText(str);
	m_ctLocDir.Invalidate();
	GetLocCurrentDirectory();
	if(!BeginFindLocFile())
		AfxMessageBox("建立网络线程失败");
	return 0L;
}

LRESULT CLocFileView::OnInsertLocFile(WPARAM wParam,LPARAM lParam)
{
	FILE_FTP_INFO* pInfo=(FILE_FTP_INFO*)wParam;
	CString str=pInfo->szFileName;
	if(str=="."||str=="..")
	{
		m_bHaveDotFlag=TRUE;
		return 0L;
	}
	OnInsertFile(&m_ctLocFile,pInfo,lParam);
	return 0L;
}
/////////////////////////////////////////////////////////////////////////////
// CMyAppThread

IMPLEMENT_DYNCREATE(CMyAppThread, CWinThread)

CMyAppThread::CMyAppThread()
{
}

CMyAppThread::~CMyAppThread()
{
}

BOOL CMyAppThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CMyAppThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMyAppThread, CWinThread)
	//{{AFX_MSG_MAP(CMyAppThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyAppThread message handlers

UINT CMyAppThread::FindLocFileThread(LPVOID lParam)
{
	CFtpParaClass:: FILE_FTP_INFO* pInfo=new (CFtpParaClass::FILE_FTP_INFO);
	CFileFind fileFind;
	UINT i=0;
	CTime time;
	BOOL bContinue;
	::SetCurrentDirectory(szLocDirectory);
	::PostMessage(pLocView->m_hWnd,WM_SETDIRECTORYDISPLAY,0,0);
	bContinue=fileFind.FindFile("*.*");
	if(!bContinue)
		return 0L;
	CString str;
	while(bContinue)
	{
		bContinue=fileFind.FindNextFile();
		if(fileFind.IsHidden())continue;
		str=fileFind.GetFileName();
		strcpy(pInfo->szFileName,str);
		fileFind.GetLastWriteTime(time);
		str=time.Format("%x");
		strcpy(pInfo->szFileDate,str);
		if(fileFind.IsDirectory())
		{
			strcpy(pInfo->szFileSize,"");
			pInfo->nType=DIRECTORYICON;
			::SendMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
				(LPARAM)LOCFILE);
		}
		else
		{
			int i=fileFind.GetLength();
			if(i>1024)
			{
				str.Format("%d",i/1024);
				str+="KB";
			}
			else
				str.Format("%d",i);
			strcpy(pInfo->szFileSize,str);
			pInfo->nType=FILEICON;
			::SendMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
				(LPARAM)LOCFILE);
		}
	}
	::SendMessage(pLocView->m_hWnd,WM_SETREDRAWFLAG,0,0);
	delete pInfo;
	fileFind.Close();
	return 0L;
}

UINT CMyAppThread::FindServFileThread(LPVOID lParam)
{
	CFtpParaClass::FILE_FTP_INFO* pInfo=new(CFtpParaClass::FILE_FTP_INFO);
	FILETIME fileTime;
	CString str,szFile,szFtpInfo;
	WIN32_FIND_DATA findData;
	HINTERNET hFind;
	HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	szFtpInfo="正在连接";
	szFtpInfo+=szFtpName;
	szFtpInfo+="服务器";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
		szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		szFtpInfo=szFtpName;
		szFtpInfo+="服务器未接上";
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		InternetCloseHandle(hInetSession);
		::Sleep(10);
		return 0L;
	}
	szFtpInfo=szFtpName;
	szFtpInfo+="服务器已连上,用户";
	szFtpInfo+=szFtpUser;
	szFtpInfo+="登录成功";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	DWORD dwLength=MAX_PATH;
	if(szFtpDirectory!=0)
		FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
	FtpGetCurrentDirectory(hFtpConn,szFtpDirectory,&dwLength);
	::PostMessage(pServView->m_hWnd,WM_SETDIRECTORYDISPLAY,0,0);
	szFtpInfo="正在查找文件";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
	{
		if (GetLastError()  == ERROR_NO_MORE_FILES) 
		{
			AfxMessageBox("目录为空,没有多余的内容");
			goto end;
		}
		else
		{
			szFtpInfo="文件查找出错,退回,请查明原因";
			pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			goto end;
		}
	}
	do{
		szFile=findData.cFileName;
		strcpy(pInfo->szFileName,szFile);
		fileTime=findData.ftLastWriteTime;
		CTime time=CTime(fileTime);
		CString str=time.Format("%x");
		strcpy(pInfo->szFileDate,str);
		if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
		{
			strcpy(pInfo->szFileSize,"");
			pInfo->nType=DIRECTORYICON;
		}
		else
		{
			DWORD i=findData.nFileSizeLow;
			if(i>1024)
			{
				str.Format("%ld",i/1024);
				str+="KB";
			}
			else
				str.Format("%ld",i);
			strcpy(pInfo->szFileSize,str);
			pInfo->nType=FILEICON;
		}
		::SendMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,(LPARAM)SERVFILE);
		
	}while(InternetFindNextFile(hFind,&findData));
	
	::SendMessage(pServView->m_hWnd,WM_SETREDRAWFLAG,0,0);
	InternetCloseHandle(hFind);
end:
	szFtpInfo="查找文件结束";
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	::Sleep(10);
	delete pInfo;
	InternetCloseHandle(hFtpConn);
	InternetCloseHandle(hInetSession);
	return 0L;	
}

UINT CMyAppThread::SendFileThread(LPVOID lParam)
{
	IsFree=TRUE;
	CFtpParaClass:: FILE_FTP_INFO* pInf=new (CFtpParaClass::FILE_FTP_INFO);
	CFtpParaClass::FILE_COUNT_INFO* pInfo=(CFtpParaClass::FILE_COUNT_INFO*)lParam;
    BOOL Result;
	CInternetSession* lpInetSession;
	CFtpConnection* lpFtpConnection;
	CString str,strFtp,OldLoc,OldFtp,szFtpInfo;
	lpInetSession=new CInternetSession(szAppName,1,PRE_CONFIG_INTERNET_ACCESS);
	if(!lpInetSession)
		return 0;
	try
	{
		lpFtpConnection=lpInetSession->GetFtpConnection(szFtpName,szFtpUser,szFtpPassword,nPort);
	}
	catch(CInternetException* lpEx)
	{
		lpEx->ReportError();
		lpEx->Delete();
		delete lpInetSession;
		return 0;
	}
	lpFtpConnection->SetCurrentDirectory(szFtpDirectory);
	::SetCurrentDirectory(szLocDirectory);
	OldLoc=szLocDirectory;
	OldFtp=szFtpDirectory;
	for(int i=0;i<nCount;i++)
	{
		str=OldLoc;
		strFtp=OldFtp;
		CString DirName=pInfo[i].fileName;
		if(pInfo[i].ufileFlag==FILEICON)
		{
			szFtpInfo="发送";
			szFtpInfo+=DirName;
			pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			if(lpFtpConnection->PutFile(DirName,DirName))
			{
				if(IsFree)
				{
					IsFree=FALSE;
					strcpy(pInf->szFileName,DirName);
					strcpy(pInf->szFileDate,"");
					strcpy(pInf->szFileSize,"");
					pInf->nType=FILEICON;
					::PostMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInf,SERVFILE);
				}
				continue;
			}
			else
			{
				szFtpInfo="发送";
				szFtpInfo+=DirName;
				szFtpInfo+="出错";
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
		else
		{
			if(str.Right(1)=="\\")
				str+=DirName;
			else
				str+="\\"+DirName;
			if(strFtp.Right(1)=='/')
				strFtp+=DirName;
			else
				strFtp+="/"+DirName;
			if(Result=PreSendFile(lpFtpConnection,str,strFtp,(LPVOID)pInfo))
				continue;
			else
			{
				AfxMessageBox("发送有错,请查明原因");
				break;
			}
		}
	}
	
	szFtpInfo="文件发送结束,退回";
	szFtpInfo+=OldFtp;
    pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	::Sleep(10);
	if(IsFree)
		::SendMessage(pServView->m_hWnd,WM_REDISPLAYFILE,(WPARAM)(LPCTSTR)OldFtp,0);
	delete[] pInfo;
	delete pInf;
	delete lpFtpConnection;
	delete lpInetSession;
	return 0L;

}


BOOL CMyAppThread::PreSendFile(CFtpConnection* lpFtp,CString& str,CString& strFtp,LPVOID pInf)
{
	CFtpParaClass:: FILE_FTP_INFO* pInfo=(CFtpParaClass::FILE_FTP_INFO*)pInf;
	BOOL result;
	UINT uSize;
	CFileFind fileFind;
	HANDLE hHeap;
	LPSTR lpBuffer;
	CString lpBufferFtp,szFtpInfo;
	CFtpConnection* lpFtpConnection=lpFtp;
	uSize=(GetCurrentDirectory(0,NULL)*sizeof(TCHAR));
	hHeap=GetProcessHeap();
	lpBuffer=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,uSize);
	lpFtpConnection->GetCurrentDirectory(lpBufferFtp);
	GetCurrentDirectory(uSize,(LPTSTR)(LPCTSTR)lpBuffer);
	if(str!=lpBuffer)
	{
		SetCurrentDirectory(str);
		szFtpInfo="创建";
		szFtpInfo+=strFtp;
		szFtpInfo+="目录";
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		lpFtpConnection->CreateDirectory(strFtp);
		lpFtpConnection->SetCurrentDirectory(strFtp);
		szFtpInfo="进入";
		szFtpInfo+=strFtp;
		szFtpInfo+="目录成功";
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		::SendMessage(pServView->m_hWnd,WM_SETDIR,0,(LPARAM)(LPCTSTR)strFtp);;
	}
	UINT bContinue=fileFind.FindFile("*");
	CString tFile;
	if(bContinue)
	{
		do
		{
			bContinue=fileFind.FindNextFile();
			tFile=fileFind.GetFileName();
			if(tFile=="."||tFile==".."||fileFind.IsHidden())continue;
			if(fileFind.IsDirectory())
			{
				if(str.Right(1)=='\\')
				{
					if(strFtp.Right(1)=='/')
						PreSendFile(lpFtpConnection,str+tFile,strFtp+tFile,(LPVOID)pInfo);
					else
						PreSendFile(lpFtpConnection,str+tFile,strFtp+"/"+tFile,(LPVOID)pInfo);
				}
				else
				{
					if(strFtp.Right(1)=='/')
						result=PreSendFile(lpFtpConnection,str+"\\"+tFile,strFtp+tFile,(LPVOID)pInfo);
					else
						result=PreSendFile(lpFtpConnection,str+"\\"+tFile,strFtp+"/"+tFile,(LPVOID)pInfo);
				}
			}
			else
			{
				szFtpInfo="发送";
				szFtpInfo+=tFile;
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				result=(lpFtpConnection->PutFile(tFile,tFile))?TRUE:FALSE;
				if(IsFree)
				{
					IsFree=FALSE;
					strcpy(pInfo->szFileName,tFile);
					strcpy(pInfo->szFileDate,"");
					strcpy(pInfo->szFileSize,"");
					pInfo->nType=FILEICON;
					::PostMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInf,SERVFILE);
				}
				continue;
			}
		}while(bContinue);
		fileFind.Close();
		SetCurrentDirectory(lpBuffer);
		szFtpInfo="退回";
		szFtpInfo+=lpBufferFtp;
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		lpFtpConnection->SetCurrentDirectory(lpBufferFtp);
		::SendMessage(pServView->m_hWnd,WM_SETDIR,0,(LPARAM)(LPCTSTR)lpBufferFtp);
		HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
		return result;
	}
	fileFind.Close();
	SetCurrentDirectory(lpBuffer);
	lpFtpConnection->SetCurrentDirectory(lpBufferFtp);
	::SendMessage(pServView->m_hWnd,WM_SETDIR,0,(LPARAM)(LPCTSTR)lpBufferFtp);
	HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
	return FALSE;
}

UINT CMyAppThread::ReceiveFileThread(LPVOID lParam)
{
	IsFree=TRUE;
	CString szFtpInfo;
	CFtpParaClass:: FILE_FTP_INFO* pInf=new (CFtpParaClass::FILE_FTP_INFO);
	CFtpParaClass::FILE_COUNT_INFO* pInfo=(CFtpParaClass::FILE_COUNT_INFO*)lParam;
	BOOL Result;
	CString str,strFtp,OldLoc,OldFtp;
	HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
		szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		AfxMessageBox("建立联接出错,返回");
		InternetCloseHandle(hInetSession);
		return 0L;
	}
	szFtpInfo="正在进入";
	szFtpInfo+=szFtpDirectory;
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
    ::SetCurrentDirectory(szLocDirectory);
	OldLoc=szLocDirectory;
	OldFtp=szFtpDirectory;
	for(int i=0;i<nCount;i++)
	{
		str=OldLoc;
		strFtp=OldFtp;
		CString DirName=pInfo[i].fileName;
		if(pInfo[i].ufileFlag==FILEICON)
		{
			szFtpInfo="正在接收";
			szFtpInfo+=DirName;
			pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			if(FtpGetFile(hFtpConn,DirName,DirName,FALSE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY |
				INTERNET_FLAG_NO_CACHE_WRITE,0))
			{
				if(IsFree)
				{
					IsFree=FALSE;
					strcpy(pInf->szFileName,DirName);
					strcpy(pInf->szFileDate,"");
					strcpy(pInf->szFileSize,"");
					pInf->nType=FILEICON;
					::PostMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInf,(LPARAM)SERVFILE);
				}
				szFtpInfo="接收";
				szFtpInfo+=DirName;
				szFtpInfo+="完毕,下一个";
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				continue;
			}
			else
			{
				szFtpInfo="接收";
				szFtpInfo+=DirName;
				szFtpInfo+="出错,退出查找原因";
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
		else
		{
			if(str.Right(1)=="\\")
				str+=DirName;
			else
				str+="\\"+DirName;
			if(strFtp.Right(1)=='/')
				strFtp+=DirName;
			else
				strFtp+="/"+DirName;
			if(Result=PreReceiveFile(hInetSession,str,strFtp,(LPVOID)pInf))
				continue;
			else
			{
				AfxMessageBox("发送有错,请查明原因");
				break;
			}
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -