[Socket] winsock2.h

// FTPClientSocket.cpp: implementation of the CFTPClientSocket class.
//
//

#include "stdafx.h"
//#include "FTPClient.h"
#include "FTPClientSocket.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//
// added by ameng
CCriticalSection CFTPClientSocket::g_csLogFile;

const int CFTPClientSocket::iTransFileUnStart=-1;
const int CFTPClientSocket::iTransFileStart=0;
const int CFTPClientSocket::iTransFileEnd=1;


//============[Response Code in FTP Server Protocol]=================================
int CFTPClientSocket::iFTPResponse_RETR = 100;
int CFTPClientSocket::iFTPResponse_REST = 100;
int CFTPClientSocket::iFTPResponse_DEL_Folder = 250;
int CFTPClientSocket::iFTPResponse_DELE_FILE = 250;
int CFTPClientSocket::iFTPResponse_CWD = 250;
int CFTPClientSocket::iFTPResponse_TransComplete = 226;
int CFTPClientSocket::iFTPResponse_Connect =  220; 
int CFTPClientSocket::iFTPResponse_DisConnec = 221;
int CFTPClientSocket::iFTPResponse_Login_User = 331;
int CFTPClientSocket::iFTPResponse_Login_Password = 230;
int CFTPClientSocket::iFTPResponse_SendFile = 226;
int CFTPClientSocket::iFTPResponse_GetCurDir = 257;
int CFTPClientSocket::iFTPResponse_CreateFolder = 257;
int CFTPClientSocket::iFTPResponse_SetCurDir = 250;
int CFTPClientSocket::iFTPResponse_TYPE = 200;
int CFTPClientSocket::iFTPResponse_PASV_Port = 227;
int CFTPClientSocket::iFTPResponse_STOR = 150;
int CFTPClientSocket::iFTPResponse_SIZE = 213;
int CFTPClientSocket::iFTPResponse_APPE = 150;


//使用以下这种New CFTPClientSocket ,可以设置类变量获得值
CFTPClientSocket::CFTPClientSocket()
{
	this->socketListen=NULL;
	this->socketSendFile=NULL;

}

CFTPClientSocket::~CFTPClientSocket()
{
//    closesocket(this->socketSendFile);
//    closesocket(this->socketListen);
//    WSACleanup();
	this->socketSendFile = NULL;
	this->socketSendFile = NULL;
}




//************************************
// Method:    ThreadFTPConnectLogin
// FullName:  CFTPClientSocket::ThreadFTPConnectLogin
// Access:    public 
// Returns:   UINT
// Qualifier: Line FTP and User PWD
// Parameter: LPVOID pParam (STRUFTPConnLogin)
//************************************
BOOL CFTPClientSocket::FTPConnectAndLoginAndTransMultiFile(STRUFTPList *pInfo)
{
  
	pInfo->bFinished = FALSE;
	pInfo->timeStart = CTime::GetCurrentTime();
	pInfo->szTimeStart = pInfo->timeStart.Format(TransTimeFormat);

	BOOL bFlag=FALSE;
	CString szOut_Message=_T("");
	
	//========================Connect FTP Server==========================================
	pInfo->szMessage = _T("Start Link Server Computer in network,Please wait a few minutes.");
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);

  	bFlag = CFTPClientSocket::ConnectServer(pInfo->szFTPServerIP,pInfo->szPort,pInfo->szMessage);

	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("link server successfully");
	}
    else
	{
		pInfo->szMessage=_T("link server Failed!");
		return 0;
	}

    CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}

	//=======================Login User ================================================
	bFlag = CFTPClientSocket::LoginUserName(pInfo->szUser,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Login successfully");
	}
    else
	{
		pInfo->szMessage=_T("User Login Failed!");
	}


	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		CFTPClientSocket::DisconnectServer(pInfo->szMessage);
		WSACleanup();
		return FALSE;
	}

	//==================== Login Password===============================================
	bFlag = CFTPClientSocket::LoginUserPWD(pInfo->szPWD,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Password is correct!");
	}
    else
	{
		pInfo->szMessage=_T("User Password is wrong!");
	}

	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		CFTPClientSocket::DisconnectServer(pInfo->szMessage);
		WSACleanup();
		return FALSE;
	}


	//====================Trans One File(Not BreakDot UploadFile)============================
    bFlag = CFTPClientSocket::FileMultiFileTrans(pInfo);
	if(FAILED(bFlag))
	{
		CFTPClientSocket::DisconnectServer(pInfo->szMessage);
		WSACleanup();
		return FALSE;
	}

	//====================Close FTP Connect =================================================
	bFlag = CFTPClientSocket::DisconnectServer(pInfo->szMessage);
	//CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	
	if(FAILED(bFlag))
	{
		WSACleanup();
		AfxMessageBox(_T("Connect Server Failed!"));
		return FALSE;
	}
	pInfo->szMessage = _T("Close Server Computer link,this trans finished!");
    pInfo->bFinished=TRUE;
	return TRUE;
}



//************************************
// Method:    ConnectServer
// FullName:  CFTPClientSocketetClient::ConnectServer
// Access:    public 
// Returns:   BOOL
// Qualifier: Connect FTP Server (PC install FTPServer software)
// Parameter: CString szFTPServerIP
// Parameter: int iPort
//************************************
BOOL CFTPClientSocket::ConnectServer(CString szServerIP,CString szPort,CString &szOutMSG)
{

	//=======Get iPort =======================================================
	int iPort=0;
	iPort = _ttoi(szPort);
	
	//========Get IP  ANSI and UNICODE is OK For IP is Simple Char =========
	char ch_ServerIP[17]={0};
    int iLen = 0;
	iLen = szServerIP.GetLength();
	for (int i=0;i<iLen;i++)
	{
		ch_ServerIP[i] = (char)szServerIP.GetAt(i); // szFTPServerIP[i]; //warring (LPCTSTR)
	}
	ch_ServerIP[iLen]='\0';  //Must be End 
	
	//==========Return Message about connect ip
	szOutMSG += _T("  Connect IP:") + szServerIP ;
	szOutMSG += _T("  Port:") + szPort;
	
	//=============ini Socket Var====================
	WSADATA data;
	if(WSAStartup(MAKEWORD(2,2), &data)!=0)
	{
		WSACleanup();
		szOutMSG += _T("\r\n WSAStartup Failed! \r\n ");
		return FALSE;
	}

	if (socketListen!=NULL)
	{
        return TRUE;
	}

	socketListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  // or IPPROTO_IP
	
	//==============FTP Parameter setting ======================
	sockaddr_in servAddr;                         //Socket Connect var
	servAddr.sin_family = AF_INET;
	servAddr.sin_port   = htons(iPort);
	servAddr.sin_addr.S_un.S_addr = inet_addr(ch_ServerIP);
	
	if(::connect(CFTPClientSocket::socketListen, (const sockaddr*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
	{
		closesocket(CFTPClientSocket::socketListen);
		WSACleanup();
		szOutMSG += _T("\r\n connect Error! ");
		return FALSE;
	}
	//delete []&ch_ServerIP; //Debug error
	//================Response if Connect successfully Message =======================
	char sendbuf[BUFF_RECV]={0};
    int i_recvCode =0 ;
	i_recvCode = recv(socketListen,sendbuf,BUFF_RECV,0);
	sendbuf[i_recvCode] = '\0';
	if(i_recvCode == SOCKET_ERROR)
	{
		closesocket(CFTPClientSocket::socketListen);
		WSACleanup();
		szOutMSG += _T("\r\n connect failed! ");
		szOutMSG += sendbuf;
		return FALSE;
	}
	
	szOutMSG += _T("\r\n   ");
	szOutMSG += sendbuf;
	
	return TRUE;
}

//************************************
// Method:    DisconnectFTPServer
// FullName:  CFTPClientSocketetClient::DisconnectFTPServer
// Access:    public 
// Returns:   int
// Qualifier: Connect not to link 
//************************************
BOOL CFTPClientSocket::DisconnectServer(CString &szMessage)
{
	BOOL bFlag=FALSE;
	CString szCommand=_T("");
//	CString szCommand=_T("");
	szCommand = _T("close\r\n");
	bFlag = SendCommandDisconnect(CFTPClientSocket::socketListen,szCommand,CFTPClientSocket::iFTPResponse_DisConnec,szMessage);

	szCommand = _T("QUIT\r\n");
	bFlag = SendCommand(CFTPClientSocket::socketListen,szCommand,CFTPClientSocket::iFTPResponse_DisConnec,szMessage);
	szMessage = _T("    ") + szMessage;
	WSACleanup();
	return bFlag;
}

//************************************
// Method:    SendCommand
// FullName:  CFTPClientSocketetClient::SendCommand
// Access:    public 
// Returns:   BOOL
// Qualifier: 发送命令+传送的信息(中文和字母)
// Parameter: SOCKET listenSock 
// Parameter: CString szCommand
// Parameter: CString & szRecvMessage OUT
//************************************
BOOL CFTPClientSocket::SendCommandDisconnect(SOCKET listenSock,CString szCommand,int iResponseCodeSuccessfully,
											 CString &szOutMSG)
{
	   
	   char sendbuf[BUFF_RECV]={0};
	   int iLength = 0;
	   BOOL bSendFlag=FALSE;
	   
	   // #ifdef _UNICODE
	   // 	   iLength = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, szCommand.GetBuffer(0),-1,sendbuf,
	   // 		   BUFF_RECV,NULL,NULL); 
	   // 	   
	   // #else
	   iLength = szCommand.GetLength();
	   for (int i=0;i<iLength;i++)
	   {
		   sendbuf[i]=(char)szCommand[i];
	   }
	   // #endif
	   
	   sendbuf[iLength]='\0';
	   //AfxMessageBox((LPCTSTR)sendbuf);
	   
	   send(listenSock, sendbuf,iLength, 0); 
	   
	   int i_recvCode =0;
	   i_recvCode = recv(listenSock,sendbuf,BUFF_RECV,0);
	   sendbuf[i_recvCode] = '\0';
	   
	   if(i_recvCode == SOCKET_ERROR || i_recvCode ==0)
	   {
		   char* chStatus = new char[1024];
#ifdef _UNICODE
		   wsprintf((LPWSTR)chStatus,_T("recv() failed: %d"), WSAGetLastError());	   
#else
		   sprintf(chStatus,_T("recv() failed: %d"), WSAGetLastError());
#endif
		   szOutMSG = chStatus;
		   delete[] chStatus;
		   bSendFlag =FALSE;
	   }
	   else
	   {
		   //判断是否和反馈码相同 iResponseCodeSuccessfully
		   CString szResponseCode=_T("");
		   szOutMSG = sendbuf;
		   szResponseCode = sendbuf;
		   szResponseCode = szResponseCode.Mid(0,3);
		   int iResponseCode=0;
		   iResponseCode = _ttoi(szResponseCode);
		   if (iResponseCode == iResponseCodeSuccessfully)
		   {
			   bSendFlag = TRUE;
		   }
		   else
		   {
			   bSendFlag = FALSE;
		   }
	   }
	   szOutMSG = sendbuf;
	   return bSendFlag;
}

//************************************
// Method:    LoginUserName
// FullName:  CFTPClientSocketetClient::LoginUserName
// Access:    public 
// Returns:   int
// Qualifier: Login User Name 
// Parameter: CString szUserName
//************************************
BOOL CFTPClientSocket::LoginUserName(CString szUserName,CString &szOutMSG)
{
	BOOL bFlag=FALSE;
	CString szCommand=_T("");
	szCommand = _T("USER ");
	szCommand += szUserName;
	szCommand +=_T("\r\n");
	
	bFlag = SendCommand(CFTPClientSocket::socketListen,szCommand,CFTPClientSocket::iFTPResponse_Login_User,szOutMSG);
	szOutMSG =_T("   ") + szOutMSG;
	return bFlag;
}

//************************************
// Method:    LoginUserPWD
// FullName:  CFTPClientSocketetClient::LoginUserPWD
// Access:    public 
// Returns:   int
// Qualifier: Login User Password
// Parameter: CString szPWD
//************************************
BOOL CFTPClientSocket::LoginUserPWD(CString szPWD,CString &szOutMSG)
{
	BOOL bFlag=FALSE;
	CString szCommand=_T("");
	szCommand = _T("PASS ");
	szCommand += szPWD;
	szCommand +=_T("\r\n");
	
	bFlag = SendCommand(CFTPClientSocket::socketListen,szCommand,CFTPClientSocket::iFTPResponse_Login_Password,szOutMSG);
	szOutMSG =_T("   ") + szOutMSG;
    return bFlag;
}


//************************************
// Method:    SendCommand
// FullName:  CFTPClientSocketetClient::SendCommand
// Access:    public 
// Returns:   BOOL
// Qualifier: 发送命令+传送的信息(中文和字母)
// Parameter: SOCKET listenSock 
// Parameter: CString szCommand
// Parameter: CString & szRecvMessage OUT
//************************************
BOOL CFTPClientSocket::SendCommand(SOCKET listenSock,CString szCommand,int iResponseCodeSuccessfully,
						   CString &szOutMSG)
{
	   
	   char sendbuf[BUFF_RECV]={0};
	   int iLength = 0;
	   BOOL bSendFlag=FALSE;
	   
#ifdef _UNICODE
	   iLength = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, szCommand.GetBuffer(0),-1,sendbuf,
		   BUFF_RECV,NULL,NULL); 
	   
#else
	   iLength = szCommand.GetLength();
	   for (int i=0;i<iLength;i++)
	   {
		   sendbuf[i]=(char)szCommand[i];
	   }
#endif
	   
	   sendbuf[iLength]='\0';
	   send(listenSock, sendbuf,iLength, 0); 
	   
	   int i_recvCode =0;
	   i_recvCode = recv(listenSock,sendbuf,BUFF_RECV,0);
	   sendbuf[i_recvCode] = '\0';
	   
	   if(i_recvCode == SOCKET_ERROR || i_recvCode ==0)
	   {
		   char* chStatus = new char[1024];
#ifdef _UNICODE
		   wsprintf((LPWSTR)chStatus,_T("recv() failed: %d"), WSAGetLastError());	   
#else
		   sprintf(chStatus,_T("recv() failed: %d"), WSAGetLastError());
#endif
		   szOutMSG = chStatus;
		   delete[] chStatus;
		   bSendFlag =FALSE;
	   }
	   else
	   {
		   //判断是否和反馈码相同 iResponseCodeSuccessfully
		   CString szResponseCode=_T("");
		   szOutMSG = sendbuf;
		   szResponseCode = sendbuf;
		   szResponseCode = szResponseCode.Mid(0,3);
		   int iResponseCode=0;
		   iResponseCode = _ttoi(szResponseCode);
		   if (iResponseCode == iResponseCodeSuccessfully)
		   {
			   bSendFlag = TRUE;
		   }
		   else
		   {
			   bSendFlag = FALSE;
		   }
	   }
	   szOutMSG = sendbuf;
	   return bSendFlag;
}

void CFTPClientSocket::logFile(int &iLineNo,CString szFileNameLog,CString szMessage,CString &szOut_Message)
{
	g_csLogFile.Lock();
	CString szline=_T("");
	szline = CFTPClientSocket::GetLogLine(iLineNo,szMessage);

	szOut_Message = szline;


	CFile file_log;
	BOOL bFlag = file_log.Open(szFileNameLog,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
	if (bFlag==FALSE)
	{
		// added by ameng
		g_csLogFile.Unlock();
		return;
	}
	
	char buffer[BUFF_RECV]={0};
	int iLength = 0;
	
#ifdef _UNICODE
	   iLength = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, szline.GetBuffer(0),-1,buffer,
		                             BUFF_RECV,NULL,NULL); 
#else
	   iLength = szline.GetLength();
	   for (int i=0;i<iLength;i++)
	   {
		   buffer[i]=(char)szline[i];
	   }
#endif	   
	   buffer[iLength]='\0';
	   file_log.SeekToEnd();
	   file_log.Write(buffer,iLength);
	   file_log.Flush(); 
	   file_log.Close();
	   // added by ameng
	   g_csLogFile.Unlock();
}

//************************************
// Method:    GetListLineText
// FullName:  CFTPClientSocketetClient::GetListLineText
// Access:    public 
// Returns:   CString
// Qualifier: Get Line Text 
// Parameter: CListBox & listBox
// Parameter: CString szNow
// Parameter: CString szMessage
//************************************
CString CFTPClientSocket::GetLogLine(int &iLineNo,CString szMessage)
{
	CString szNow=_T("");
	//CTime is 2037 later not use.
	//COleDateTime is ok 

#if _DEBUG
	//由于缺少MFCO42.DLL文件,所以用这个来获得时间和日期,
    CTime timeDate=CTime::GetCurrentTime();
	szNow = timeDate.Format(_T("%H:%M:%S %A, %B %d, %Y"));
#else
	COleDateTime *dateTime=new COleDateTime() ;//= COleDateTime::GetCurrentTime();
	dateTime = &COleDateTime::GetCurrentTime();
	szNow = dateTime->Format(_T("%H:%M:%S %A, %B %d, %Y"));
#endif

	CString szLine=_T("");
	CString szCount = _T("");
	iLineNo++;
	szCount.Format(_T("%d"),iLineNo);
	if (iLineNo == 1)
	{
		szLine = _T("[");
	}
	else
	{
		szLine = _T("\r\n[");
	}
	szLine += szCount;
	szLine += _T("]");
	szLine += szNow;
	szLine += _T("\r\n   ");
	szLine += szMessage;
	return szLine;
}



BOOL CFTPClientSocket::DelServerFiles(STRUFTPList *pInfo)
{
	pInfo->bFinished = FALSE;
	pInfo->timeStart = CTime::GetCurrentTime();
	pInfo->szTimeStart = pInfo->timeStart.Format(TransTimeFormat);
	
    int iFileCount=0;
	iFileCount = pInfo->struFileArray.GetSize();
	BOOL bFlag = FALSE;
	for (int iFilePos=0; iFilePos<iFileCount; iFilePos++)
	{
		//AfxMessageBox(_T("execute del server files"));

		bFlag = DelServerFile(&pInfo->struFileArray[iFilePos],pInfo->szFTPServerIP,pInfo->iLineNoMessage,pInfo->szFileLog);

		if (bFlag==FALSE)
		{
			break;
		}
		bFlag=TRUE;
	}
	pInfo->bFinished=TRUE;
    return bFlag;
}

BOOL CFTPClientSocket::FileMultiFileTrans(STRUFTPList *pInfo)
{
	pInfo->bFinished = FALSE;
	pInfo->timeStart = CTime::GetCurrentTime();
	pInfo->szTimeStart = pInfo->timeStart.Format(TransTimeFormat);

    int iFileCount=0;
	iFileCount = pInfo->struFileArray.GetSize();
	BOOL bFlag = FALSE;

    //1. 如果是上传,就在这里建立文件夹,
	CString szFolderRemote=_T("");
	CString szTempLog=_T("");
	

	if (pInfo->bIsDownload==FALSE)
	{
		if (pInfo->struFileArray.GetSize()>0)
		{
			szFolderRemote = pInfo->struFileArray[0].szFolderRemote;
			//由于建立路径,如果存在,再建立就失败,如果二级路经,还是将失败,必须一级一级的建立
			//AfxMessageBox(szFolderRemote);

			//建立一级路径 XML
			if (pInfo->iFolderRemoteProgression==1 && CFTPClientSocket::CreateFolderInServer(szFolderRemote,szTempLog)==FALSE)
			{
				
			}
  
			//建立二级路经 Pic,MP3,avi
			if (pInfo->iFolderRemoteProgression==2)
			{
				CString szFolder=_T("");
				szFolder = szFolderRemote;
				szFolder = szFolderRemote.Left(szFolderRemote.GetLength()-1);
				CString szFolder_1 = _T("");
				int iPos=0;
				iPos = szFolder.ReverseFind(_T('/'));
				szFolder_1 = szFolder.Mid(0,iPos);
				//AfxMessageBox(szFolder_1);
				CFTPClientSocket::CreateFolderInServer(szFolder_1,szTempLog);
				CFTPClientSocket::CreateFolderInServer(szFolderRemote,szTempLog);
			}
		}
		else
		{
			return FALSE;
		}
	}
    

	//2. 将文件夹路径设置好 Download and Upload 在同一个路径下
	szFolderRemote = pInfo->struFileArray[0].szFolderRemote;
    CString szCWDRemoteDirectory = _T("CWD ") + szFolderRemote +_T("\r\n");
	//AfxMessageBox(szCWDRemoteDirectory);
	bFlag=CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCWDRemoteDirectory,CFTPClientSocket::iFTPResponse_CWD,szTempLog); 
	CString szMessage=_T("");
	if (szTempLog.Mid(0,3)==_T("250"))
	{
		szMessage = _T("Go to directory is successfully!");
	}
	else
	{
	    szMessage = _T("CWD Path is Not Exist");
 		//return FALSE;
	}


	for (int iFilePos=0; iFilePos<iFileCount; iFilePos++)
	{
		pInfo->struFileArray[iFilePos].iStatus = CFTPClientSocket::iTransFileStart;
		 if (pInfo->bIsDownload==TRUE)
		 {
            if(pInfo->struFileArray[iFilePos].szFileNameRemote.GetLength()>0)
			{
 	            bFlag = FileDownload(&pInfo->struFileArray[iFilePos],pInfo->szFTPServerIP,pInfo->iLineNoMessage,pInfo->szFileLog);
			}
		 }
		 else
		 {
			 if(pInfo->struFileArray[iFilePos].szFileFullNameLocal.GetLength()>0)
			 {
              bFlag = FileUpload(&pInfo->struFileArray[iFilePos],pInfo->szFTPServerIP,pInfo->iLineNoMessage,pInfo->szFileLog);
			 }
		 }

		 if (bFlag==FALSE)
		 {
		  	 break;
		 }
		 bFlag=TRUE;
		 pInfo->struFileArray[iFilePos].iStatus = CFTPClientSocket::iTransFileEnd;
	}
	pInfo->bFinished=TRUE;
    return bFlag;
}

BOOL CFTPClientSocket::FileUpload(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog)
{
	CString szTempLog=_T("");
	CString szOutMsg=_T("");
	CString szCommandSendFile=_T("");
	BOOL bFlag=FALSE;
	pFile->timeStart = CTime::GetCurrentTime();
	pFile->szTimeStart = pFile->timeStart.Format(TransTimeFormat);
	pFile->iStatus = CFTPClientSocket::iTransFileStart;

	//[第一步] [获得本地文件大小] 主要是断点续传 或者显示 UI 传送量信息
	pFile->iFileSizeLocal = CFTPClientSocket::GetFileSizeInt(pFile->szFileFullNameLocal);

	//显示在UI信息上
	pFile->szFileSizeLocal.Format(_T("%s"),CFTPClientSocket::GetFileSizeConvent(pFile->iFileSizeLocal));
	// 如果在服务器上,建立指定的路径,开始设定好默认的路径,然后在之上建立文件夹,必定会成功,
	// 失败说明已经建立了这个文件夹
	//AfxMessageBox(pFile->szFolderRemote);
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szFolderRemote,szTempLog);
 
// 	if (CFTPClientSocket::CreateFolderInServer(pFile->szFolderRemote,szTempLog)==FALSE)
// 	{
// 	    pFile->szMessage =_T("Create Folder Failed in server");
// 		//AfxMessageBox(pFile->szMessage);
// 	}
// 	else
// 	{
// 		pFile->szMessage = _T("Create Folder in Successfully!");
// 		//AfxMessageBox(pFile->szMessage);
// 		CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
// 	}
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	//到这个路径目录中去
//     CString szCWDRemoteDirectory = _T("CWD ") + pFile->szFolderRemote +_T("\r\n");
// 	   bFlag=CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCWDRemoteDirectory,CFTPClientSocket::iFTPResponse_TYPE,szTempLog); 
// 	//TYPE A UNICODE.TXT I E
// 	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,szTempLog,szOutMsg);
// 	if (szTempLog.Mid(0,3)==_T("250"))
// 	{
// 		pFile->szMessage = _T("Go to directory is successfully!");
// 	}
// 	else
// 	{
// 	    pFile->szMessage = _T("CWD Path is Not Exist");
//  		//return FALSE;
// 	}
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	
	if (pFile->szFileNameRemote.GetLength()<=0)
	{
		pFile->szFileNameRemote = CFileOperator::GetFileName(pFile->szFileFullNameLocal);
	}
	//AfxMessageBox(pFile->szFileNameRemote);

	 CString szTEMP=_T("");
	 szTEMP.Format(_T("%d"),pFile->bIfReTrans);
	 //AfxMessageBox(szTEMP);

	//return 0;
	if (pFile->bIfReTrans==FALSE)
	{ //不重新传递==断点续传
		//AfxMessageBox(_T("pFile->bIfReTrans==FALSE"));
		
		//=========[ Get FTPServer File Size ] =============================================
		szCommandSendFile = _T("SIZE ") + pFile->szFileNameRemote;
		szCommandSendFile +=_T("\r\n");
		CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_SIZE,pFile->szMessage);
		pFile->szFileSizeRemote = pFile->szMessage.Mid(4,pFile->szMessage.GetLength()-4);
		pFile->iFileSizeRemote = _ttoi(pFile->szFileSizeRemote); 

		//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
		if (pFile->iFileSizeRemote == pFile->iFileSizeLocal)
		{
			//已经传送完毕
			pFile->szMessage = _T("This File Size = FTP Server File Size.");
			CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
			return TRUE;
		}
		else
		{
			//Break Dot Upload File ,从断点位置,开始上传数据
			pFile->iFilePos = pFile->iFileSizeRemote;
		}
	}
	else
	{
		//重新上传数据
		pFile->iFilePos = 0; //表示从新传递,文件位置从0开始
		pFile->iFileSizeRemote = 0;
	}

	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,_T("TYPE A\r\n"),CFTPClientSocket::iFTPResponse_TYPE,pFile->szMessage); //TYPE A UNICODE.TXT I E
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	CString szNewIPPort=_T("");
	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,_T("PASV \r\n"),CFTPClientSocket::iFTPResponse_PASV_Port,szNewIPPort);//PASV I
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,szNewIPPort,szTempLog);
	
    //Create Socket For UPLOAD FILE
	CreateSocket(socketSendFile,szFTPServerIP,szNewIPPort);
	
	//STOR UpdateFile Command  Before must be create socketSend
	if (pFile->bIfReTrans==TRUE)
	{
		szCommandSendFile = _T("STOR ") + pFile->szFileNameRemote +_T("\r\n");
	}
	else
	{
		szCommandSendFile = _T("APPE ") + pFile->szFileNameRemote +_T("\r\n");
	}
	
	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_STOR,pFile->szMessage);
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	//return FALSE;
	//Sleep(1000);

	bFlag = CFTPClientSocket::TransFileToServer(socketSendFile,pFile,iLineNoMessage,szFileLog);
	pFile->szMessage=_T("Trans File to server finished");
	pFile->iStatus = CFTPClientSocket::iTransFileEnd;
	
	return bFlag;
}

//UNICODE and DEBUG All OK
BOOL CFTPClientSocket::TransFileToServer(SOCKET &mlistenSock,STRUFile *pSTRUFile,int iLineNoMessage,CString szFileLog)
{
	int iSendTotal= 0;
	iSendTotal = pSTRUFile->iFileSizeRemote;
	int iSendCount =BUFF_UPLOAD;
	INT iCount=0;
	
 	//====================计算出百分数中每份的数据==========================
 	LPCTSTR filename= pSTRUFile->szFileFullNameLocal;
	DOUBLE iPerUnitSize = pSTRUFile->iFileSizeLocal / pSTRUFile->iPercentMax;
	DOUBLE iPerUnitSizeRest = pSTRUFile->iFileSizeLocal % pSTRUFile->iPercentMax * 0.01;
	iPerUnitSize+= iPerUnitSizeRest ;
	DOUBLE iPercent=0;
	CString szPercent=_T("");
	CString szMessageOut=_T("");
	
    //===================打开本地文件传递===================================
	CString szMessage=_T("");
	CFile cfile;
	int nRead= BUFF_UPLOAD ;	
	char buffer[BUFF_UPLOAD]={0};
	//BOOL bFileOpenFlag = cfile.Open(filename, CFile::modeRead | CFile::modeNoTruncate); 
	//AfxMessageBox(filename);
	 BOOL bFileOpenFlag = cfile.Open(filename, CFile::modeRead | CFile::shareDenyWrite);  //By ameng

	if (pSTRUFile->iFilePos<=0)
	{
		pSTRUFile->iFilePos=0;
	}

    if (bFileOpenFlag==FALSE)
    {
		pSTRUFile->szMessage  =_T("Can not Open File:");
		pSTRUFile->szMessage  += filename;
		CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pSTRUFile->szMessage,szMessageOut);
		closesocket(mlistenSock);
		return FALSE;
    }
	else
	{
		int iRestSize=0;
		CString szTemp=_T("");
		CString szSpeed=_T("");
		int iSpeed=0;
		 LONG nTSeconds=0;

		int iPos = cfile.Seek(pSTRUFile->iFilePos,SEEK_CUR);
		while(nRead == BUFF_UPLOAD )
		{
		
			iCount++;
			if (iCount==2)
			{
			//	Sleep(pSTRUFile->iDelaySleep);//上传不需要延迟,可以达到每秒40多k
			}
			nRead = cfile.Read(buffer, BUFF_UPLOAD);
            iSendCount = send(mlistenSock,buffer,nRead,0);
			if (iSendCount == SOCKET_ERROR)
			{
                break; 
			}
			iSendTotal +=iSendCount;
			iRestSize = pSTRUFile->iFileSizeLocal - iSendTotal;

			//计算百分号
			iPercent = iSendTotal / iPerUnitSize; //iPercentSize;
			if (iPercent==20)
			{
// 				CString szValue;
// 				AfxMessageBox(_T("20"));
			}
			szPercent.Format((LPCTSTR)_T("%.2f%%"),iPercent);
			pSTRUFile->iPercent = iPercent;
			pSTRUFile->szPercent= szPercent;
			if (pSTRUFile->pListCtrl!=NULL)
			{
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,3,pSTRUFile->szPercent);
			}

			pSTRUFile->timeEnd = CTime::GetCurrentTime();
			pSTRUFile->szTimeTrans=CFTPClientSocket::GetTimeSpanTIMEFormat(pSTRUFile->timeStart);

		
			//==========求速度==============================================

			// 两个CTime相减得到CTimeSpan
			CTimeSpan timeSpan = pSTRUFile->timeEnd - pSTRUFile->timeStart;
			// 得到总的秒数
             nTSeconds = timeSpan.GetTotalSeconds();
			 if (iSendTotal>0 && nTSeconds>0)
			 {
				iSpeed =  iSendTotal / nTSeconds;
			 }
			szSpeed.Format(_T("%s /s"),CFTPClientSocket::GetFileSizeConvent(iSpeed));
			pSTRUFile->szSpeed = szSpeed;
			if (pSTRUFile->pListCtrl!=NULL)
			{
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,3,pSTRUFile->szPercent);
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,5,pSTRUFile->szTimeTrans);
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,6,pSTRUFile->szSpeed);
			}
			//Show in Control
			pSTRUFile->szSizeTrans = CFTPClientSocket::GetFileSizeConvent(iSendTotal);
			pSTRUFile->szSizeRest = CFTPClientSocket::GetFileSizeConvent(iRestSize);
		}
	}
	cfile.Close();  
	closesocket(mlistenSock);
 	BOOL  bSendFlag =FALSE;
	// 得到是否传送成功信息
	bSendFlag = CFTPClientSocket::GetFTPServerResponseSuccessful(CFTPClientSocket::iFTPResponse_TransComplete,szMessage);
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pSTRUFile->szMessage,szMessageOut);
	pSTRUFile->szMessage=_T("Trans Data Finished!");
	CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pSTRUFile->szMessage,szMessageOut);
	return bSendFlag;
}

BOOL CFTPClientSocket::CreateSocket(SOCKET &createSocket,CString szFTPServerIP,CString szPortMessage)
{
	int iNewPort = 0;
	iNewPort = GetSendFilePortFormFTPServer(szPortMessage);
	
	SOCKADDR_IN l_sockaddr;
	l_sockaddr.sin_family = AF_INET;
	l_sockaddr.sin_port = htons(iNewPort);
	
	//========Get IP  ========================================================
	char ch_ServerIP[100]={0};
    int iLen = szFTPServerIP.GetLength();
	for (int i=0;i<iLen;i++)
	{
		ch_ServerIP[i] = (char)szFTPServerIP[i];
	}
	ch_ServerIP[iLen]='\0';  //Must be End
	
	l_sockaddr.sin_addr.S_un.S_addr = inet_addr(ch_ServerIP);
	createSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	connect(createSocket,(struct sockaddr *)&l_sockaddr,sizeof(l_sockaddr));
	return TRUE;
}

int  CFTPClientSocket::GetFileSizeInt(CString szFileName)
{
	CString szMessage=_T("");
	int iFileSize=0;
	CString szFileSize=_T("");
	//获得本地文件大小
	HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, 
								OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		szMessage.Format(_T("\r\n  Open File: %s is Failed!"),szFileName);
		return 0;
	}
	// 取到本地文件大小,防止传送Server的 File位置 >本地文件,
	iFileSize= GetFileSize(hFile, NULL); //hFile 自有的属性
	CloseHandle(hFile);
	return iFileSize;
}
CString CFTPClientSocket::GetFileSizeConvent(int iDataSize)
{
	if (iDataSize<=0)
	{
		return _T("0");
	}
	DOUBLE iFileSize=0;
	DOUBLE iUnit= 1024;
	CString szFileSize=_T("0");
	
	DOUBLE iMSize=0;
	DOUBLE iKSize=0;
	DOUBLE iBSize=0;
	
	iMSize =  iDataSize / iUnit /iUnit;
	if (iMSize>1)
	{
		szFileSize.Format((LPCTSTR)_T("%.2fMB"),iMSize);
		return szFileSize;
	}
	iKSize =  iDataSize / iUnit ;
	if (iKSize>1)
	{
		szFileSize.Format((LPCTSTR)_T("%.2fKB"),iKSize);
		return szFileSize;
	}
	
	iBSize =  iDataSize ;
	if (iBSize>0)
	{
		szFileSize.Format((LPCTSTR)_T("%.2fB"),iBSize);
		return szFileSize;
	}
	
	return szFileSize;
}
// if (szCurDir.GetLength()==1)
// {
// 	szCurDir=_T("/");
// }
// CString szServerCurDir=_T("");
// 
// CString szCommand =_T("");
// szCommand =  _T("MKD ") +szCurDir;
// szCommand += _T("/") + szFolder;
// szCommand += _T("\r\n");
// CString szTemp=_T("");
// 
// BOOL bFlag = SendCommand(CFTPSock::socketListen,szCommand,CFTPSock::iFTPResponse_CreateFolder,szTemp);
// szOutMSG =_T("\r\n   ") + szTemp;
// 
// 	return bFlag;

BOOL CFTPClientSocket::CreateFolderInServer(CString szRemoteFolder,CString &szOutMSG)
{
	if (szRemoteFolder.GetLength()==1)
	{
		szRemoteFolder=_T("/");
	}
	CString szServerCurDir=_T("");
	
	CString szCommand =_T("");
	szCommand =  _T("MKD ") +szRemoteFolder;
	//szCommand += _T("/") + szFolder;
	szCommand += _T("\r\n");
	//AfxMessageBox(szCommand);
	CString szTemp=_T("");
	
	BOOL bFlag = SendCommand(CFTPClientSocket::socketListen,szCommand,CFTPClientSocket::iFTPResponse_CreateFolder,szTemp);
	szOutMSG =_T("\r\n   ") + szTemp;
	
	return bFlag;
}

int CFTPClientSocket::GetSendFilePortFormFTPServer(CString szNewPort)
{
	int iPort = 0;
	int iMid =256;
	CString szPortMain=_T("");
	CString szPortAdd=_T("");
	int iCount=0;
	int iPos=0;
	CString szValue = szNewPort;
	CString szTemp=_T("");
	szTemp = szNewPort.Mid(0,3);
	if (szTemp!=_T("227"))
	{
		return -1;
	}
	for (int i=0;i<szValue.GetLength();i++)
	{
        iPos = szValue.Find(_T(","));
		if (iPos>-1)
		{
			iCount++;
			szValue = szValue.Mid(iPos+1);
		}
		if (iCount == 4)
		{
			// szPortMain 
			szPortMain = szValue.Mid(0,szValue.Find(_T(",")));
			//AfxMessageBox(szPortMain);
		}
		if (iCount == 5)
		{
			// szPortAdd
			szPortAdd = szValue.Mid(0,szValue.GetLength()-3);
			//AfxMessageBox(szPortAdd);
			break;
		}
	}
	int iPortMain=0;
	int iPortAdd=0;
	iPortMain = _ttoi(szPortMain);
	iPortAdd = _ttoi(szPortAdd);
	iPort = iPortMain * iMid;
	iPort +=iPortAdd;
	return iPort;
}


CString CFTPClientSocket::GetTimeSpan(CTime timeStart, CString szHourSign, CString szMinSign, CString szSecSign)
{
	int iSecondsRest=0;
	int iOneHoursSec=60*60;
	int iOneMinSec=60;
	CString szTemp=_T("");
	
	CString szHour=_T("");
	CString szMin=_T("");
	CString szSec=_T("");
	
	CTime timeEnd = CTime::GetCurrentTime();
	
    // 两个CTime相减得到CTimeSpan
    CTimeSpan timeSpan = timeEnd - timeStart;
	
    // 得到总的秒数
    int nTSeconds = timeSpan.GetTotalSeconds();
	int iSec = 0;
	int iMin = 0;
	int iHours =0;
	CString szTimeSpan=_T("");
	iHours = nTSeconds/iOneHoursSec;
	iSecondsRest = nTSeconds - iHours*iOneHoursSec;
	
	//Hours
	if (iHours>0)
	{
		szTemp.Format(_T("%d"),iHours);
		szHour = szTemp + szHourSign;
	}
	//Second
    iMin = iSecondsRest/iOneMinSec; 
	iSecondsRest = nTSeconds - iMin*iOneMinSec;
	if (iMin>0)
	{
		//AfxMessageBox(szMin);
		szTemp.Format(_T("%d"),iMin);
		szMin = szTemp + szMinSign;
	}
	
	iSec = iSecondsRest;
	if (iSec>0)
	{
		szTemp.Format(_T("%d"),iSec);
		szSec = szTemp + szSecSign;
	}
	
    szTimeSpan = szHour +szMin +szSec;
	// 	if (szTimeSpan.GetLength()<=0)
	// 	{
	// 		szTimeSpan.Format(_T("%d"),nTSeconds);
	// 		szTimeSpan +=szSecSign;
	// 	}
	return szTimeSpan;
}

BOOL CFTPClientSocket::GetFTPServerResponseSuccessful(int iResponseCodeSuccessfully,CString &szOutMessage)
{
	//得到是否传送成功信息
	char sendbuf[100];
	int i_recvCode =0;
	i_recvCode = recv(CFTPClientSocket::socketListen,sendbuf,BUFF_RECV,0);
	sendbuf[i_recvCode] = '\0';
	szOutMessage +=_T("\r\n ");
	szOutMessage += sendbuf;
	
    BOOL  bSendFlag =FALSE;
	if(i_recvCode == SOCKET_ERROR || i_recvCode ==0)
	{
		BOOL  bSendFlag =FALSE;
	}
	else
	{
		//判断是否和反馈码相同 iResponseCodeSuccessfully
		CString szResponseCode=_T("");
		szResponseCode = sendbuf;
		szResponseCode = szResponseCode.Mid(0,3);
		int iResponseCode=0;
		iResponseCode = _ttoi(szResponseCode);
		if (iResponseCode == iResponseCodeSuccessfully)
		{
			bSendFlag = TRUE;
		}
		else
		{
			bSendFlag = FALSE;
		}
	}
	return bSendFlag;
}


BOOL CFTPClientSocket::FileDownload(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog)
{
	CString szCommandSendFile=_T("");
	CString szMessage=_T("");
	CString szNewIPPort=_T("");
	CString szOutMessage=_T("");

	BOOL bFlag=FALSE;
	CString szTempLog=_T("");
	//获得本地文件大小
    pFile->timeStart =CTime::GetCurrentTime();

	//检测本地文件是否存在
	
	if (pFile->bIfReTrans==TRUE)
	{
		//删除这个文件,然后建立这个文件
		BOOL bFileExist = CFTPClientSocket::IsExistFile(pFile->szFileFullNameLocal);
		if (bFileExist==TRUE)
		{
			DeleteOneFile(pFile->szFileFullNameLocal);
		}
		CreateNewFile(pFile->szFileFullNameLocal);
	} 

	pFile->iFileSizeLocal=CFTPClientSocket::GetFileSizeInt(pFile->szFileFullNameLocal);
	CString szFileSize=_T("");
	pFile->szFileSizeLocal.Format(_T("%s"),CFTPClientSocket::GetFileSizeConvent(pFile->iFileSizeLocal));

	//到这个路径目录中去
    CString szCWDRemoteDirectory = _T("CWD ") + pFile->szFolderRemote +_T("\r\n");
	
	bFlag=CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCWDRemoteDirectory,CFTPClientSocket::iFTPResponse_TYPE,szMessage); 
	//TYPE A UNICODE.TXT I E
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	if (szMessage.Mid(0,3)==_T("250"))
	{
		pFile->szMessage = _T("Go to directory is successfully!");
	}
	else
	{
		AfxMessageBox(_T("Server Path is Not Exist"));
		return FALSE;
	}

	szCommandSendFile = _T("SIZE ") + pFile->szFileNameRemote;
	szCommandSendFile +=_T("\r\n");
	
	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_SIZE,szMessage);
	if (szMessage.Mid(0,3)==_T("550"))
	{
		szTempLog =pFile->szFileNameRemote+_T(" not exist in server!");
		pFile->szMessage = szTempLog;
		CFTPClientSocket::logFile(iLineNoMessage,szFileLog,szTempLog,szOutMessage);
		return FALSE;
	}

	CString szFiseSizeMSG=_T("");
	szFiseSizeMSG = szMessage;
	
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	CString szFTPServerFileSize = szFiseSizeMSG.Mid(4,szFiseSizeMSG.GetLength()-4);
	int iFTPServerFileSize = _ttoi(szFTPServerFileSize); 
	pFile->iFileSizeRemote = iFTPServerFileSize;
	pFile->szFileSizeLocal.Format(_T("%s"),CFTPClientSocket::GetFileSizeConvent(iFTPServerFileSize));
	
	if (pFile->iFileSizeRemote == pFile->iFileSizeLocal)
	{
		//已经传送完毕
		szMessage = _T("This File Size = FTP Server File Size.");
		CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
		return TRUE;
	}
	else
	{
		//Break Dot Upload File
		if (pFile->bIfReTrans==FALSE)
		{
			pFile->iFilePos = pFile->iFileSizeLocal;
			// pInfo->iFileSizeRemote = iFTPServerFileSize;
		}
		else
		{
			pFile->iFilePos = 0; //表示从新传递,文件位置从0开始
  		    //pInfo->iFileSizeRemote =iFTPServerFileSize;
		}
	}
	
	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,_T("TYPE A\r\n"),CFTPClientSocket::iFTPResponse_TYPE,szMessage); //TYPE A UNICODE.TXT I E
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,_T("PASV \r\n"),CFTPClientSocket::iFTPResponse_PASV_Port,szMessage);//PASV I
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	szNewIPPort = szMessage;
	
    //Create Socket For UPLOAD FILE
	CreateSocket(socketSendFile,szFTPServerIP,szNewIPPort);
	
	//STOR UpdateFile Command  Before must be create socketSend
	if (pFile->bIfReTrans==TRUE )
	{
	}
	else
	{
		// Setting FTP FileLocation Pos
		CString szFileSizeLocal=_T("");
		szFileSizeLocal.Format(_T("%d"),pFile->iFileSizeLocal);
		szCommandSendFile = _T("REST ") + szFileSizeLocal+_T("\r\n");
		CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_REST,szMessage);
		//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	}
	szCommandSendFile = _T("RETR ") + pFile->szFileNameRemote +_T("\r\n");
	CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_STOR,szMessage);
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);

	bFlag = CFTPClientSocket::TransFileToLocal(socketSendFile,pFile,iLineNoMessage,szFileLog);
	return bFlag;
}

//UNICODE and DEBUG All OK
BOOL CFTPClientSocket::TransFileToLocal(SOCKET &mlistenSock,STRUFile *pSTRUFile,int iLineNoMessage,CString szFileLog)
{
	int iSendTotal=pSTRUFile->iFileSizeLocal;
	int iSendCount = BUFF_DOWNLOAD;
	INT iCount=0;
	CString szMessageOut=_T("");
	
	//====================计算出百分数中每份的数据==========================
	//LPCTSTR filename= pSTRUFile->szFileNameFullPathLocal;
	DOUBLE iPerUnitSize = pSTRUFile->iFileSizeRemote / pSTRUFile->iPercentMax;
	DOUBLE iPerUnitSizeRest = pSTRUFile->iFileSizeRemote % pSTRUFile->iPercentMax * 0.01;
	iPerUnitSize+= iPerUnitSizeRest ;
	DOUBLE iPercent=0;
	CString szPercent=_T("");
	
    //===================打开本地文件传递===================================
	CString szMessage=_T("");
	int nRead= BUFF_DOWNLOAD ;	
	char buffer[BUFF_DOWNLOAD+1]={0};
    BOOL bFileOpenFlag=FALSE;
	char chFile[BUFF_DOWNLOAD+1]={0};
	//char chFile[BUFF_DOWNLOAD]={0};
	int iLength = 0;
	BOOL bSendFlag=FALSE;
	CString szFileName = pSTRUFile->szFileFullNameLocal;
	   
#ifdef _UNICODE
	   iLength = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, 
		                              szFileName.GetBuffer(0),-1,chFile,
									  BUFF_DOWNLOAD,NULL,NULL); 
#else
	   iLength = szFileName.GetLength();
	   for (int i=0;i<iLength;i++)
	   {
		   chFile[i]=(char)szFileName[i];
	   }
#endif
	chFile[iLength]='\0';
	int iRestSize=0;
	CString szTemp=_T("");
	ofstream cfile;
	if (pSTRUFile->bIfReTrans==FALSE)
	{
	   cfile.open(chFile, ios::binary|ios::app);
	}
	else
	{
	   cfile.open(chFile, ios::binary);
	}

	if (pSTRUFile->iFilePos<=0)
	{
		pSTRUFile->iFilePos=0;
	}
	pSTRUFile->szMessage = _T("start trans file..... please wait a few minutes");
	CString szSpeed=_T("");
	int iSpeed=0;
	while(1)
    {
        // Wait to receive, nRet = NumberOfBytesReceived
        nRead = recv(mlistenSock, buffer, sizeof(buffer), 0);
		
        if (nRead == SOCKET_ERROR)
        {
            break;
        }
        if (nRead == 0)
        {
            break;
        }
        else
        {
			buffer[nRead]='\0';
			cfile.write(buffer, nRead);
			cfile.flush();
			iSendCount = nRead;
			iSendTotal += iSendCount;
            if( (iSendTotal>1024) && (iSendTotal/1024>0))
            {
			iSendCount = nRead;
			iSendTotal +=iSendCount;
			iRestSize = pSTRUFile->iFileSizeRemote- iSendTotal;
			//计算百分号
			iPercent = iSendTotal / iPerUnitSize; //iPercentSize;
			szPercent.Format((LPCTSTR)_T("%.2f%%"),iPercent);
			pSTRUFile->iPercent=iPercent;
			pSTRUFile->szPercent= szPercent;
		
            //[Span Time]
			pSTRUFile->timeEnd = CTime::GetCurrentTime();
// 			szTemp=CFTPClientSocket::GetTimeSpan(pSTRUFile->timeStart,pSTRUFile->szHourSign,
// 			                         	pSTRUFile->szMinSign,pSTRUFile->szSecSign);
			szTemp=CFTPClientSocket::GetTimeSpanTIMEFormat(pSTRUFile->timeStart);

			if (szTemp.GetLength()>0)
			{
				pSTRUFile->szTimeTrans = szTemp;
			}
			//[Speed]
			CTimeSpan timeSpan = pSTRUFile->timeEnd - pSTRUFile->timeStart;
            LONG nTSeconds = timeSpan.GetTotalSeconds();
			if (iSendTotal>0 && nTSeconds>0)
			{
				iSpeed =  iSendTotal / nTSeconds;
			}

			szSpeed.Format(_T("%s /s"),CFTPClientSocket::GetFileSizeConvent(iSpeed));
			pSTRUFile->szSpeed = szSpeed;
			
			if (pSTRUFile->pListCtrl!=NULL)
			{
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,3,pSTRUFile->szPercent);
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,5,pSTRUFile->szTimeTrans);
				pSTRUFile->pListCtrl->SetItemText(pSTRUFile->iListCtrlRowIndex,6,pSTRUFile->szSpeed );
			}

			//Show in Control
			pSTRUFile->szSizeTrans = CFTPClientSocket::GetFileSizeConvent(iSendTotal);
			pSTRUFile->szSizeRest = CFTPClientSocket::GetFileSizeConvent(iRestSize);
			}
		}
	}

	pSTRUFile->szMessage = _T("start trans file finished!");
	cfile.close();  
	closesocket(mlistenSock);
	// 得到是否传送成功信息
	bSendFlag = CFTPClientSocket::GetFTPServerResponseSuccessful(CFTPClientSocket::iFTPResponse_TransComplete,
														pSTRUFile->szMessage);
	//CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pSTRUFile->szMessage,szMessageOut);
	return bSendFlag;
}

//************************************
// Method:    DeleteOneFile
// FullName:  CFileOperator::DeleteOneFile
// Access:    public 
// Returns:   BOOL
// Qualifier: Delete Folder Test Successfully!
// Parameter: CString File
//************************************
BOOL  CFTPClientSocket::DeleteOneFile(CString File)     
{   
	if(DeleteFile((LPCTSTR)File)==TRUE)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CFTPClientSocket::CreateNewFile(CString szFileName)
{
	CFile  file; 
	//Open File 
	if (file.Open(szFileName, CFile::modeCreate|CFile::modeNoTruncate)==FALSE)
	{
		return FALSE;
	}
	
    file.Close();
	return TRUE;
}

//************************************
// Method:    IsExistFile
// FullName:  CFileOperator::IsExistFile
// Access:    public 
// Returns:   BOOL
// Qualifier: 是否存在文件,检测文件是否存在
// Parameter: CString szFileName
//************************************
BOOL CFTPClientSocket::IsExistFile(CString szFileName)
{
	BOOL  bFlag = FALSE;
	LPWIN32_FIND_DATA lpFindFileData = new WIN32_FIND_DATA;
	HANDLE hfile = FindFirstFile(szFileName,lpFindFileData); 
	delete lpFindFileData;
	if(hfile==INVALID_HANDLE_VALUE)
	{
		bFlag = FALSE;
	}
	else
	{
		bFlag = TRUE;
	}
    FindClose(hfile);   
    return bFlag;
}

//************************************
// Method:    GetCreateFileNameUID
// FullName:  GetCreateFileNameUID
// Access:    public 
// Returns:   CString
// Qualifier:
// Parameter: CString szID
//************************************
CString CFTPClientSocket::GetCreateFileNameUID(CString szID)
{
   CString szUID=_T("");
   CTime tiem_now=CTime::GetCurrentTime();
   CString szTime=_T("");
   CString szMinSecond=_T("");
   szTime = tiem_now.Format(TimeDate);
   DWORD minSecond = timeGetTime();
   szMinSecond.Format(_T("%000d"),minSecond);
   szUID = szTime+szMinSecond+szID;
   return szUID;
}

BOOL CFTPClientSocket::FTPConnectAndLogin(STRUFTPList *pInfo)
{
	pInfo->bFinished = FALSE;
	pInfo->timeStart = CTime::GetCurrentTime();
	pInfo->szTimeStart = pInfo->timeStart.Format(TransTimeFormat);
	
	BOOL bFlag=FALSE;
	CString szOut_Message=_T("");
	
	//========================Connect FTP Server==========================================
	//pInfo->szMessage = _T("Start Link Server Computer in network,Please wait a few minutes.");
	pInfo->szMessage.Format(_T("%s%s%s"),pInfo->szFTPServerIP,pInfo->szPort,pInfo->szMessage);
	//AfxMessageBox(pInfo->szMessage);

	bFlag = CFTPClientSocket::ConnectServer(pInfo->szFTPServerIP,pInfo->szPort,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("link server successfully");
		//AfxMessageBox(pInfo->szMessage);
		CFTPClientSocket::bLinkServer=TRUE;
	}
	else
	{
		//AfxMessageBox(pInfo->szMessage);
		pInfo->szMessage=_T("link server Failed!");
	//	AfxMessageBox(pInfo->szMessage);
		return 0;
	}
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	//=======================Login User ================================================
	bFlag = CFTPClientSocket::LoginUserName(pInfo->szUser,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Login successfully");
	}
	else
	{
		pInfo->szMessage=_T("User Login Failed!");
	}
	
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	//==================== Login Password===============================================
	bFlag = CFTPClientSocket::LoginUserPWD(pInfo->szPWD,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Password is correct!");
		//AfxMessageBox(pInfo->szMessage);
	}
	else
	{
		pInfo->szMessage=_T("User Password is wrong!");
	}
	
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	return TRUE;
}

BOOL CFTPClientSocket::FTPDisconnect(STRUFTPList *pInfo)
{
	//====================Close FTP Connect =================================================
	BOOL bFlag = CFTPClientSocket::DisconnectServer(pInfo->szMessage);
	CString szOut_Message=_T("");
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		AfxMessageBox(_T("Connect Server Failed!"));
		return FALSE;
	}
	pInfo->szMessage = _T("Close Server Computer link,this trans finished!");
	return TRUE;
}


// if(WSAGETSELECTEVENT(lParam)   ==   FD_ACCEPT)//如果 
// { 
// 	Client   =   accept(ServerSocket,(LPSOCKADDR)&m_sockClientAddr,0); 
// 	AfxBeginThread(AcceptThreadProc,(LPVOID)&Client,0,0,0,NULL); 
// 	================================================================ 
// 		上面这句中的,Client   是什么变量? 
// 		
// 		UINT   CServerDlg::AcceptThreadProc(LPVOID   pParam) 
// 	{ 
// 		SOCKET   ClientSocket=(SOCKET)(LPVOID)pParam;   
// 		================================================================= 
// 			你这是个怎么样的转换过程?将一个地址强制转换成一个SOCKET对象? 

// 谢谢大家,问题解决了.我在ACCEPT里加入了SELECT,摘录代码如下: 
// fd_set rset,allset; 
// maxfd=msock; 
// FD_ZERO(&allset); 
// FD_SET(msock,&allset); 
// for(;;) 
// { 
// 	rset=allset; 
// 	ready=select(maxfd+1,&rset,NULL,NULL,&time); 
// 	if(FD_ISSET(msock,&rset) 
// 		ssock=accept()... 
//         FD_SET(ssock,&allset); 
// 	if(ssock>maxfd) 
// 		maxfd=ssock; 
// 	
// } 



BOOL CFTPClientSocket::FTPConnectAndLoginAndDelServerFile(STRUFTPList *pInfo)
{
	pInfo->bFinished = FALSE;
	BOOL bFlag=FALSE;
	CString szOut_Message=_T("");
	
	//========================Connect FTP Server==========================================
	pInfo->szMessage = _T("Start Link Server Computer in network,Please wait a few minutes.");
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	
	bFlag = CFTPClientSocket::ConnectServer(pInfo->szFTPServerIP,pInfo->szPort,pInfo->szMessage);
	
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("link server successfully");
	}
    else
	{
		pInfo->szMessage=_T("link server Failed!");
		return 0;
	}
	
    CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	//=======================Login User ================================================
	bFlag = CFTPClientSocket::LoginUserName(pInfo->szUser,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Login successfully");
	}
    else
	{
		pInfo->szMessage=_T("User Login Failed!");
	}
	
	
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	//==================== Login Password===============================================
	bFlag = CFTPClientSocket::LoginUserPWD(pInfo->szPWD,pInfo->szMessage);
	if (bFlag==TRUE)
	{
		pInfo->szMessage=_T("User Password is correct!");
	}
    else
	{
		pInfo->szMessage=_T("User Password is wrong!");
	}
	
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	
	//====================Trans One File(Not BreakDot UploadFile)============================
    bFlag = CFTPClientSocket::DelServerFiles(pInfo);
	if(FAILED(bFlag))
	{
		WSACleanup();
		return FALSE;
	}
	
	//====================Close FTP Connect =================================================
	bFlag = CFTPClientSocket::DisconnectServer(pInfo->szMessage);
	CFTPClientSocket::logFile(pInfo->iLineNoMessage,pInfo->szFileLog,pInfo->szMessage,szOut_Message);
	
	if(FAILED(bFlag))
	{
		WSACleanup();
		AfxMessageBox(_T("Connect Server Failed!"));
		return FALSE;
	}
	pInfo->szMessage = _T("Close Server Computer link,this trans finished!");
    pInfo->bFinished=TRUE;
	return TRUE;
}

BOOL CFTPClientSocket::DelServerFile(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog)
{
	CString szCommandSendFile=_T("");
	BOOL bFlag=FALSE;
	CString szTempLog=_T("");

	//Go to Dir in FTPServer
    CString szCWDRemoteDirectory = _T("CWD ") + pFile->szFolderRemote +_T("\r\n");
	
	bFlag=CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCWDRemoteDirectory,CFTPClientSocket::iFTPResponse_TYPE,pFile->szMessage); 
	//TYPE A UNICODE.TXT I E
	CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	if (pFile->szMessage.Mid(0,3)==_T("250"))
	{
		pFile->szMessage = _T("Go to directory is successfully!");
	}
	else
	{
		AfxMessageBox(_T("Server Path is Not Exist"));
		return FALSE;
	}

	//DELE FileName in FTPServer
	szCommandSendFile = _T("DELE ") + pFile->szFileNameRemote;
	szCommandSendFile +=_T("\r\n");

	CFTPClientSocket::logFile(iLineNoMessage,szFileLog,szCommandSendFile,szTempLog);
	bFlag = CFTPClientSocket::SendCommand(CFTPClientSocket::socketListen,szCommandSendFile,CFTPClientSocket::iFTPResponse_SIZE,pFile->szMessage);
	if (pFile->szMessage.Mid(0,3)==_T("250"))
	{
		bFlag=TRUE;
	}
	else
	{
       bFlag=FALSE;
	}
	CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	pFile->szMessage=_T("Delete File:")+ pFile->szFileNameRemote;
	CFTPClientSocket::logFile(iLineNoMessage,szFileLog,pFile->szMessage,szTempLog);
	return bFlag;
}

// 00:00:00 形式显示
CString CFTPClientSocket::GetTimeSpanTIMEFormat(CTime timeStart)
{
	int iSecondsRest=0;
	int iOneHoursSec=60*60;
	int iOneMinSec=60;
	CString szTemp=_T("");
	
	CString szHour=_T("");
	CString szMin=_T("");
	CString szSec=_T("");
	
	CTime timeEnd = CTime::GetCurrentTime();
	
    // 两个CTime相减得到CTimeSpan
    CTimeSpan timeSpan = timeEnd - timeStart;
	
    // 得到总的秒数
    int nTSeconds = timeSpan.GetTotalSeconds();
	int iSec = 0;
	int iMin = 0;
	int iHours =0;
	CString szTimeSpan=_T("");
	iHours = nTSeconds/iOneHoursSec;
	iSecondsRest = nTSeconds - iHours*iOneHoursSec;
	
	//Hours
	if (iHours>0)
	{
		szTemp.Format(_T("%d"),iHours);
		szHour = szTemp ;
		if (szHour.GetLength()<=1)
		{
			szHour = _T("0")+szHour;
		}
	}
	else
	{
		szHour =_T("00");
	}
	//Second
    iMin = iSecondsRest/iOneMinSec; 
	iSecondsRest = nTSeconds - iMin*iOneMinSec;
	if (iMin>0)
	{
		szTemp.Format(_T("%d"),iMin);
		szMin = szTemp ;
		if (szMin.GetLength()<=1)
		{
			szMin = _T("0")+szMin;
		}
	}
	else
	{
		szMin =_T("00");
	}
	
	iSec = iSecondsRest;
	if (iSec>0)
	{
		szTemp.Format(_T("%d"),iSec);
		szSec = szTemp;
		if (szSec.GetLength()<=1)
		{
			szSec = _T("0")+szSec;
		}
	}
	else
	{
		szSec =_T("00");
	}
	
	
    szTimeSpan = szHour+_T(":") +szMin +_T(":")+szSec;
	return szTimeSpan;
}

step1

H

// FTPClientSocket.h: interface for the CFTPClientSocket class.
// Function : Client Connet FTP Server(IIS6,Server-U and so on [FTP Protocol])
// [6/18/2009 ]
//

#if !defined(AFX_FTPCLIENTSOCKET_H__A82E3628_1CAB_4B30_9036_33324B83AADF__INCLUDED_)
#define AFX_FTPCLIENTSOCKET_H__A82E3628_1CAB_4B30_9036_33324B83AADF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//=============== SOCKET ==========================
#include <winsock2.h> 
#pragma comment(lib, "WS2_32.LIB")	

//======CFile [Upload file and download file]
#include <fstream.h> 

//====== For File UID=DateTimeMinSecond ->timeGetTime Function
#include   <mmsystem.h> 
#pragma comment(lib,"Winmm.lib")


//FTP char buffer[] Array Size
#define  BUFF_RECV         1*1024
#define  BUFF_UPLOAD       8*1024
//#define  BUFF_DOWNLOAD     8191

#define  BUFF_DOWNLOAD     4095
// 以上的速度是:6K/s 其他软件测试 18k/S 800K FIle 600 200
//4*1024-1
//#define  BUFF_DOWNLOAD     2047 
// 以上的速度是:9K/s 其他软件测试 18k/S 800K FIle 600 200

//#define  BUFF_DOWNLOAD     1*1023 
// 以上的速度是:4K/s 其他软件测试 6k/S

#define  BUFFER_BLOCK_SIZE 1*1024

//Exception Happen Error
//#define CheckERR(x) if (FAILED(hr=(x))) {CFTPSock::Msg(TEXT("FAILED(hr=0x%x) in ") TEXT(#x) TEXT("\n\0"), hr); return hr;}

#define  TransTimeFormat  _T("%H:%M:%S")
#define  TimeDate _T("%Y%m%d%H%M%S")

class CFTPClientSocket  
{
     //Attributes
public:
	 const static int iTransFileUnStart;
	 const static int iTransFileStart;
	 const static int iTransFileEnd;

	 BOOL bLinkServer;
	 // added by ameng
	 static CCriticalSection g_csLogFile;


	//===========[FTP Link Comm Var]==================================
	 CString m_szFTPIP; // [*]
	 CString m_szFTPPort; // [*]
	 CString m_szFTPUserName; // [*]
	 CString m_szFTPUserPWD; // [*]
     CString m_szLogFileName; // Local FTP Log
	
	 //============[Response Code in FTP Server Protocol]=================================
	 static int   iFTPResponse_DEL_Folder;
	 static int   iFTPResponse_RETR;
	 static int   iFTPResponse_REST;
	 static int   iFTPResponse_Login_User ;
	 static int   iFTPResponse_Login_Password;
	 static int   iFTPResponse_Connect;
	 static int   iFTPResponse_DisConnec;
	 static int   iFTPResponse_SendFile;
	 static int   iFTPResponse_GetCurDir;
	 static int   iFTPResponse_CreateFolder;
	 static int   iFTPResponse_SetCurDir;
	 static int   iFTPResponse_TYPE;
	 static int   iFTPResponse_PASV_Port;
	 static int   iFTPResponse_STOR;
	 static int   iFTPResponse_TransComplete;
	 static int   iFTPResponse_SIZE;
	 static int   iFTPResponse_DELE_FILE;
	 static int   iFTPResponse_APPE;
	 static int   iFTPResponse_CWD;

	/************************************************************************/
	/* 传递一个文件的文件属性 */
	/************************************************************************/
	 struct STRUFile
	{
		//===Local File Attributes==================
		CString szFileFullNameLocal;     // Full Path and File Name
		CString szFileDescLocal;         // File Description
		CString szFileSizeLocal;
		int iFileSizeLocal;

		//===Remote File Attributes==================
		CString szFolderRemote;
		CString szFileSizeRemote;
		int iFileSizeRemote;

		CString szFileNameRemote; // 只有文件名和后缀名的 ServerFileName=LocalFileName

		//===Trans Information=========================
		CString szSizeTrans;      // 已经传送的文件大小
		CString szSizeRest;       // 剩余文件大小

		int iFilePos;

		CString szTimeStart;          //Default NULL;
		CTime timeStart;
		CString szTimeEnd;            //Default NULL;
		CTime timeEnd;
		CString szTimeTrans;
		
		//Percent Information
		DOUBLE iPercent;         //  Default=0;
		CString szPercent;       // Show
		int iPercentMax;         //  Default = 100;
		int iPercentMin;         //  Default = 0;

		BOOL bIfReTrans; //Default TRUE is ReTrans,
		CString szMessage;
		int iDelaySleep; //must be =0;

		//===============Time Sign ============================================
		CString szHourSign;// Default:Hours
		CString szMinSign; // Default:Minutes
		CString szSecSign; // Default:Second
		CListCtrl *pListCtrl;
		int iListCtrlRowIndex;
		int iStatus; // [-1:UnStart] [0:Start][1:Finished] Trans File
		CString szSpeed;
	};

	/************************************************************************/
	/* 传送文件的主要结构体 */
	/************************************************************************/
	 struct STRUFTPList
	{
		//=====Connect and Login==============================================
		CString szFTPServerIP;
		CString szUser;
		CString szPWD;
		CString szPort;
		
		BOOL bIsDownload;            // True = Download else Upload
		CString szIsDownload_Caption; //表示当前在下在或者在上传数据
        
		//==============Log Show===============================================
        CString szFileLog;   // 日志文件,如果是NULL,表示不要日志
		int iLineNoMessage;  // 日志的初始化 =0
		CString szMessage;   // 日志的每行信息,或者外界显示当前传递的情况

		//===============Time Sign ============================================
		CString szHourSign;// Default:Hours
		CString szMinSign; // Default:Minutes
		CString szSecSign; // Default:Second

		CString szTimeStart;          //Default NULL;
		CTime timeStart;
		CString szTimeEnd;            //Default NULL;
		CTime timeEnd;
		CString szTimeTrans;
		
		//Percent Information
		DOUBLE iPercent;         //  Default=0;
		CString szPercent;       // Show
		int iPercentMax;         //  Default = 100;
		int iPercentMin;         //  Default = 100;
		CArray<STRUFile,STRUFile&> struFileArray;
		//CXTPReportControl *pReportControl;
		CListCtrl *pListCtrl;
		BOOL bFinished;
		int iFolderRemoteProgression; //文件夹级数[1级存放文件XML,二级存放Pic,MP3等]
			
	};

	
//protected:
	 SOCKET socketSendFile;
	 SOCKET socketListen; 

public:
	CFTPClientSocket();
	virtual ~CFTPClientSocket();

	//这个函数适合一次连接+登陆+传输多个文件[Upload and download]
    BOOL FTPConnectAndLoginAndTransMultiFile(STRUFTPList *pInfo);

	/************************************************************************/
	/* 以下三个函数的调用过程
	   第一步:FTPConnectAndLogin [启动的时候,或者要传输文件前操作]
	   第二步:FileMultiFileTrans
	          [多次调用本函数]
	   第三步:FTPDisconnect [当关闭系统的时候]
	   分别调用
	*/
	/************************************************************************/
	BOOL FTPConnectAndLoginAndDelServerFile(STRUFTPList *pInfo);

	// 连接和断开
	BOOL FTPConnectAndLogin(STRUFTPList *pInfo);
	BOOL FTPDisconnect(STRUFTPList *pInfo);

	//传送文件函数
	BOOL FileMultiFileTrans(STRUFTPList *pInfo);
 
	 //Others
	 int GetFileSizeInt(CString szFileName);
	 CString GetFileSizeConvent(int iDataSize);
	 CString GetTimeSpan(CTime timeStart, CString szHourSign, CString szMinSign, CString szSecSign);

	 // 00:00:00
	 CString GetTimeSpanTIMEFormat(CTime timeStart);
	 BOOL DeleteOneFile(CString File);
	 BOOL CreateNewFile(CString szFileName);
	 BOOL IsExistFile(CString szFileName);
	 CString GetCreateFileNameUID(CString szID);
	
private:	
	 //Open FTP Server
	 BOOL ConnectServer(CString szServerIP,CString szPort,CString &szOutMSG);
	 BOOL DisconnectServer(CString &szMessage);
	 BOOL LoginUserName(CString szUserName,CString &szOutMSG);
	 BOOL LoginUserPWD(CString szPWD,CString &szOutMSG);
	 BOOL SendCommand(SOCKET listenSock,CString szCommand,int iResponseCodeSuccessfully, CString &szOutMSG);
	 BOOL CreateFolderInServer(CString szRemoteFolder,CString &szOutMSG);
	 BOOL CreateSocket(SOCKET &createSocket,CString szFTPServerIP,CString szPortMessage);
	 int  GetSendFilePortFormFTPServer(CString szNewPort);
	 BOOL GetFTPServerResponseSuccessful(int iResponseCodeSuccessfully,CString &szOutMessage);
   
	//Upload 单个文件
	 BOOL FileUpload(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog);
	 BOOL TransFileToServer(SOCKET &mlistenSock,STRUFile *pSTRUFile,int iLineNoMessage,CString szFileLog);
	
    //Download 单个文件
	 BOOL FileDownload(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog);
	 BOOL TransFileToLocal(SOCKET &mlistenSock,STRUFile *pSTRUFile,int iLineNoMessage,CString szFileLog);

	 //Delete file in FTP server
	 BOOL DelServerFiles(STRUFTPList *pInfo);
	 BOOL DelServerFile(STRUFile *pFile,CString szFTPServerIP,int iLineNoMessage,CString szFileLog);
	
	//日志文件[得到移行数据]
	 CString GetLogLine(int &iLineNo,CString szMessage);
	//写一行细心到日志中去
	 void logFile(int &iLineNo,CString szFileNameLog,CString szMessage,CString &szOut_Message);
	 BOOL SendCommandDisconnect(SOCKET listenSock,CString szCommand,int iResponseCodeSuccessfully, CString &szOutMSG);
	 
	 
};

#endif // !defined(AFX_FTPCLIENTSOCKET_H__A82E3628_1CAB_4B30_9036_33324B83AADF__INCLUDED_)


CPP


OK!


“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值