FtpLib.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:28k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // Ftp.cpp: implementation of the CFtp class.
- //
- //////////////////////////////////////////////////////////////////////
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "FtpLib.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFtpLib::CFtpLib()
- {
- m_bRun = true ;
- }
- CFtpLib::~CFtpLib()
- {
- }
- /***********************************************************
- ** @Description:
- ** connect the ftp server .
- ** @Note:
- ** LPCTSTR lpszHostName : the host address .
- **
- ** @Return: the connected socket handle .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- SOCKET CFtpLib::ConnectServer( LPCTSTR lpszHostName )
- {
- LPHOSTENT lpHostent ;
- LPSERVENT lpServent ;
- SOCKADDR_IN sockAddr ;
- WORD nPort ;
- int bConnect ;
- int iWinsockErr ;
- SOCKET hConnectSocket ;
- //get host ip address .
- if( !(lpHostent = gethostbyname( lpszHostName ) ) )
- {
- iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while get server ip addr!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return INVALID_SOCKET ;
- }
- if( ( hConnectSocket = socket( AF_INET , SOCK_STREAM , 0 ) ) == INVALID_SOCKET )
- {
- iWinsockErr = WSAGetLastError() ;
- sprintf( m_szFtpBuffer , " %d error while create connect socket!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return INVALID_SOCKET ;
- }
- //get default ftp host port .
- lpServent = getservbyname( "ftp" , 0 ) ;
- if( lpServent == NULL )
- nPort = htons( DEFAULT_FTP_PORT ) ;
- else
- nPort = lpServent->s_port ;
- //set server address .
- sockAddr.sin_family = AF_INET ;
- sockAddr.sin_port = nPort ;
- sockAddr.sin_addr = *((LPIN_ADDR)*lpHostent->h_addr_list ) ;
- bConnect = connect( hConnectSocket , (LPSOCKADDR)&sockAddr , sizeof( SOCKADDR_IN ) ) ;
- if( bConnect == SOCKET_ERROR )
- {
- iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error while connect server !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return INVALID_SOCKET ;
- }
- //check the socket is ready to read .
- //IsReadyToRead( hConnectSocket ) ;
- //get the server reply code .
- UINT nRelpyCode = GetServerReply( hConnectSocket ) ;
- if( nRelpyCode >= 400 || nRelpyCode <= 0 )
- {
- //closesocket( hConnectSocket ) ;
- return hConnectSocket ;
- }
- else
- {
- return hConnectSocket ;
- }
- }
- /***********************************************************
- ** @Description:
- ** check the socket connect is ready to read ?
- **
- ** @Note:
- ** SOCKET hSocket : socket handle want be check .
- **
- ** @Return: if ready to read true ,else false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpLib::IsReadyToRead(SOCKET hSocket)
- {
- FD_SET ReadSet ;
- TIMEVAL timeout ;
- int nReady ;
- int iWinsockErr ;
- //set time.
- //wait one second .
- timeout.tv_sec = 1 ;
- timeout.tv_usec = 0 ;
- FD_ZERO( &ReadSet ) ;
- FD_SET( hSocket , &ReadSet ) ;
- //select the socket .
- nReady = select( 0 , &ReadSet , 0 , 0 , &timeout ) ;
- if( nReady == SOCKET_ERROR )
- {
- iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while select function read !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return FALSE ;
- }
- //check socket .
- if( FD_ISSET( hSocket , &ReadSet ) )
- {
- return TRUE ;
- }
- else
- return FALSE ;
- }
- /***********************************************************
- ** @Description:
- ** receive the reply string from server .
- **
- ** @Note:
- ** SOCKET hSocket : the connected socket handle.
- **
- ** @Return: the server reply code.
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- ***********************************************************/
- int CFtpLib::GetServerReply(SOCKET hSocket)
- {
- char szBuffer[1024] ;
- int iLength ;
- int iBuffer = 0 ;
- BOOL bNeedReceive = true ;
- char szMultiLineHead[MAX_PATH] ;
- UINT nReplyCode ;
- memset( szMultiLineHead , 0 ,MAX_PATH ) ;
- while( bNeedReceive )
- {
- memset( szBuffer , 0 , 1024 ) ;
- iBuffer = 0 ;
- //because the reply string possibly is multi line .
- //so we must peek the reply info before receive it.
- //peek the message .
- if( ( iLength = recv( hSocket , szBuffer , 1024 , MSG_PEEK ) ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while recv server reply!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- if( iLength <= 0 )
- return (ERROR_FTP_CODE );
- //debug
- //Log the file .
- /*
- CFile file ;
- file.Open( "e:\receive.txt" , CFile::modeWrite ) ;
- file.SeekToEnd( ) ;
- file.Write( szBuffer , iLength ) ;
- file.Close() ;
- */
- //find the line end .
- while( szBuffer[iBuffer] != 'n' )
- {
- iBuffer ++ ;
- if( iBuffer >= iLength )
- break ;
- }
- //skip the 'n'
- iBuffer ++ ;
- TRACE( "%drn" , iLength ) ;
- //receive server reply string .
- if( ( iLength = recv( hSocket , szBuffer , iBuffer , 0 ) ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while recv server reply!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- if( iLength < 1024 )
- szBuffer[iLength] = 0 ;
- else
- szBuffer[1023] = 0 ;
- //notify the parent wnd.
- SendMsgToParentWnd( szBuffer , false ) ;
- if( strlen( szMultiLineHead ) == 0 )
- {
- nReplyCode = GetReplyCode( szBuffer ) ;
- itoa( (int)nReplyCode , szMultiLineHead , 10 ) ;
- if( szBuffer[3] == '-' )
- bNeedReceive = true ;
- else
- bNeedReceive = false ;
- }
- else
- {
- if( strncmp( szMultiLineHead , szBuffer , 3 ) == 0 )
- {
- if( szBuffer[3] == '-' )
- bNeedReceive = true ;
- else
- bNeedReceive = false ;
- }
- else
- bNeedReceive = true ;
- }
- }
- nReplyCode = GetReplyCode ( szBuffer ) ;
- return nReplyCode ;
- }
- /***********************************************************
- ** @Description:
- ** Get server reply command code.
- **
- ** @Note:
- ** LPSTR lpszBuffer : the reply string .
- **
- ** @Return: server reply code.
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::GetReplyCode(LPSTR lpszBuffer)
- {
- //get the server reply code ,
- //the ftp server will return a string after receive command ,
- //first three char in this string is the server reply code .
- UINT nCode ;
- char cBak ;
- cBak = *(lpszBuffer + 3 );
- *( lpszBuffer + 3 ) = 0 ;
- nCode = atoi( lpszBuffer ) ;
- *(lpszBuffer + 3 ) = cBak ;
- return nCode ;
- }
- /***********************************************************
- ** @Description:
- ** send a command to ftp server ,and receive the reply
- **
- ** @Note:
- ** LPSTR szCommandBuffer : the command string
- ** SOCKET hSocket : the connected socket handle.
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::SendFtpCommand(LPSTR szCommandBuffer , SOCKET hSocket )
- {
- if( !m_bRun )
- return ERROR_FTP_CODE ;
- //send command . and after send will check server reply .
- if( send( hSocket , szCommandBuffer , strlen( szCommandBuffer ) , 0 ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while Send ftp command !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- //Send msg to window .
- SendMsgToParentWnd( szCommandBuffer , true ) ;
- return GetServerReply( hSocket ) ;
- }
- /***********************************************************
- ** @Description:
- ** close the ftp control connect socket handle .
- *
- ** @Note:
- ** SOCKET hSocket : the connected socket handle .
- **
- ** @Return: true .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpLib::CloseFtpConnect( SOCKET hSocket )
- {
- closesocket( hSocket ) ;
- return TRUE ;
- }
- /***********************************************************
- ** @Description:
- ** Create the data link .
- **
- ** @Note:
- ** SOCKET hControlSocket : the control connected socket handle .
- ** @Return: if success return the data listen socket handle ,
- ** else return INVALID_SOCKET .
- **
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- //this function is used to create date link .
- SOCKET CFtpLib::CreateListenCannel(SOCKET hControlSocket)
- {
- SOCKADDR_IN sockaddr ;
- SOCKET hListenSocket ;
- if( ( hListenSocket = socket( AF_INET , SOCK_STREAM , 0 ) ) == INVALID_SOCKET )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when create listen socket !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- //make address.
- sockaddr.sin_family = AF_INET ;
- sockaddr.sin_port = htons( 0 ) ;
- sockaddr.sin_addr.s_addr = INADDR_ANY ;
- if( bind( hListenSocket , (LPSOCKADDR)&sockaddr , sizeof( sockaddr ) ) )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when bind the listen socket !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- if( listen( hListenSocket , 1 ) )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when listening " , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- return PrepareDataCannel( hListenSocket , hControlSocket ) ;
- }
- /***********************************************************
- ** @Description:
- ** prepare the data cannel , transmit the data.
- **
- ** @Note:
- ** SOCKET hListenSocket : Listen data socket hanle .
- ** SOCKET hControlSocket : Control socket handle .
- **
- ** @Return: the data link connect socket .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- SOCKET CFtpLib::PrepareDataCannel(SOCKET hListenSocket, SOCKET hControlSocket)
- {
- SOCKADDR_IN sockaddr ;
- int iLength ;
- UINT nPort ;
- UINT nReplyCode ;
- iLength = sizeof( sockaddr ) ;
- // Get the address for the hListenSocket
- if( getsockname( hListenSocket , (LPSOCKADDR)&sockaddr , &iLength ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when get listen socket name!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- nPort = sockaddr.sin_port ;
- iLength = sizeof( sockaddr ) ;
- if( getsockname( hControlSocket , ( LPSOCKADDR )&sockaddr , &iLength ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when get control socket name!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- //use PORT command to tell server connect this socket ,
- //after server receive this command , server will create a socket to connect
- //this port .
- char szCommand[MAX_PATH];
- sprintf( szCommand , "PORT %d,%d,%d,%d,%d,%drn" ,
- sockaddr.sin_addr.S_un.S_un_b.s_b1,
- sockaddr.sin_addr.S_un.S_un_b.s_b2,
- sockaddr.sin_addr.S_un.S_un_b.s_b3,
- sockaddr.sin_addr.S_un.S_un_b.s_b4,
- nPort&0xFF ,
- nPort >> 8 );
- //send command .
- if( ( nReplyCode = SendFtpCommand( szCommand , hControlSocket ) ) != 200 )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when Send ftp command !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- else
- return ( hListenSocket ) ;
- }
- /***********************************************************
- ** @Description:
- ** Accept data connect socket .
- **
- ** @Note:
- ** SOCKET hListenSocket : the data listen socket handle .
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- SOCKET CFtpLib::AcceptDataConnect(SOCKET hListenSocket)
- {
- SOCKET hDataConnect = 0 ;
- SOCKADDR_IN sockaddr ;
- int iLength ;
- iLength = sizeof( sockaddr ) ;
- //accept the data socket .
- hDataConnect = accept( hListenSocket , (LPSOCKADDR)&sockaddr , &iLength ) ;
- if( hDataConnect == INVALID_SOCKET )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , "%d error when accept data connect !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( INVALID_SOCKET ) ;
- }
- return ( hDataConnect ) ;
- }
- /***********************************************************
- ** @Description:
- **
- ** @Note:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::ReadDataCannel(SOCKET hControlSocket, SOCKET hDataSocket, char *Buffer)
- {
- return 1;
- }
- /***********************************************************
- ** @Description:
- ** read the data from data connect and , wirte it to
- ** local file .
- **
- ** @Note:
- ** SOCKET hControlSocket : the control socket handle .
- ** SOCKET hDataSocket : the data connect socket handle .
- ** HANDLE hFile : the write file handle.
- ** __int64 iFileSize : file transmit file size .
- **
- ** @Return: after transmit complete , the server return code
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::ReadDataCannelToFile( SOCKET hControlSocket, SOCKET hDataSocket, HANDLE hFile , __int64 iFileSize )
- {
- DWORD nByteRec = 0 ;
- __int64 lDataNum = 0 ;
- char ReceiveBuffer[2048] ;
- m_hDataSocket = hDataSocket ;
- int iProgress = 0 ;
- int iSpeed = 0 ;
- //get start time .
- DWORD dwStart = GetTickCount( ) ;
- DWORD dwEnd = dwStart ;
- //receive data from net , and write it to local file .
- do
- {
- //receive data.
- nByteRec = recv( hDataSocket , ReceiveBuffer , 2048 , 0 ) ;
- if( nByteRec == SOCKET_ERROR )
- break ;
- if( nByteRec > 0 )
- {
- //total receive bytes.
- lDataNum += nByteRec ;
- if( !WriteFile( hFile , ReceiveBuffer , nByteRec , &nByteRec , NULL ) )
- {
- break ;
- }
- //make progress percent.
- if( iFileSize != 0 )
- iProgress = ( int )( lDataNum * 100 / iFileSize ) ;
- else
- iProgress = 100 ;
- //get end time .
- dwEnd = GetTickCount() ;
- //get transmit speed .
- iSpeed = GetTransmitSpeed( dwStart , dwEnd , lDataNum ) ;
- ::PostMessage( m_hParentWnd , FTP_TRANSMIT_DATA_MSG , iProgress , iSpeed ) ;
- }
- }
- while( nByteRec > 0 );
- //close data socket handle .
- closesocket( hDataSocket ) ;
- m_hDataSocket = NULL ;
- if( nByteRec == SOCKET_ERROR )
- return false ;
- return ( GetServerReply( hControlSocket ) ) ;
- }
- /***********************************************************
- ** @Description:
- ** global function , use for initialize socket lib .
- **
- ** @Note:
- ** void
- **
- ** @Return: success return true ,else false.
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL InitSocket( )
- {
- WSADATA wsadata ;
- if( WSAStartup( MAKEWORD( 1 , 1 ) , &wsadata ) )
- {
- return false ;
- }
- else
- {
- return true ;
- }
- }
- /***********************************************************
- ** @Description:
- ** global function , uninit the socket lib.
- **
- ** @Note:
- ** void .
- **
- ** @Return: success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL UnInitSocket( )
- {
- if( WSACleanup( ) )
- {
- return false ;
- }
- else
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** upload a file to server .
- **
- ** @Note:
- ** SOCKET hControlSocket : connected control socket .
- ** SOCKET hDataSocket : connected data socket .
- ** HANDLE hFile : the open file handle .
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::WriteDataCannelFromFile(SOCKET hControlSocket, SOCKET hDataSocket, HANDLE hFile )
- {
- DWORD nByteRec = 0 ;
- DWORD nByteSend = 0 ;
- char SendBuffer[RECEIVE_SIZE] ;
- //backup data socket handle .
- m_hDataSocket = hDataSocket ;
- __int64 nFileSize = 0 ;
- __int64 nSendSize = 0 ;
- DWORD dwFileSizeHigh = 0 ;
- DWORD dwFileSizeLow = 0 ;
- int iPrecent = 0 ;
- int iSpeed = 0 ;
- //get start time
- DWORD dwStart = GetTickCount( ) ;
- DWORD dwEnd = dwStart;
- //get file size .
- dwFileSizeLow = GetFileSize( hFile , &dwFileSizeHigh ) ;
- nFileSize = dwFileSizeHigh ;
- nFileSize = dwFileSizeLow + (nFileSize << 32 ) ;
- //transmit data.
- do
- {
- //read data from file.
- if( ReadFile( hFile , SendBuffer , RECEIVE_SIZE , &nByteRec , NULL ) )
- {
- if( nByteRec > 0 )
- {
- //send data.
- nByteSend = send( hDataSocket , SendBuffer , nByteRec , 0 ) ;
- if( nByteSend <= 0 || nByteSend == SOCKET_ERROR )
- {
- break ;
- }
- //total transmit bytes
- nSendSize += nByteRec ;
- //get end time .
- dwEnd = GetTickCount() ;
- //get transmit precent.
- iPrecent = (int) ( nSendSize * 100 / nFileSize );
- //get transmit speed .
- iSpeed = GetTransmitSpeed( dwStart , dwEnd , nSendSize ) ;
- ::PostMessage( m_hParentWnd , FTP_TRANSMIT_DATA_MSG , iPrecent , iSpeed ) ;
- //dwStart = GetTickCount( ) ;
- }
- else
- break ;
- }
- }
- while( nByteRec > 0 );
- //close handle .
- closesocket( hDataSocket ) ;
- m_hDataSocket = NULL ;
- if( nByteSend == SOCKET_ERROR )
- return false ;
- return ( GetServerReply( hControlSocket ) ) ;
- }
- /***********************************************************
- ** @Description:
- ** this function is parse the server file list info.
- ** before you can understand this function ,you must
- ** know the ftp server reply file list format.
- **
- ** @Note:
- ** int * pos[in/out] : the pointer position of file list string.
- ** char* word[out] : the buffer use for store current word.
- ** int iwordsize[in] : the buffer size .
- ** CString* string[in] : the file list string .
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- UINT CFtpLib::GetSubItem(int *pos, char *word , int iwordsize , CString* string )
- {
- int iPos = *pos ;
- int iLength = string->GetLength() ;
- //zero memory
- memset( word , 0 , iwordsize ) ;
- //if pos overtop the string length .
- if( iPos >= ( iLength - 1 ) )
- return 2 ;
- //skip the space char .
- while( string->GetAt( iPos ) == ' ' )
- {
- iPos ++ ;
- if( iPos >= ( iLength - 1 ) || string->GetAt( iPos ) == 'r' )
- return 0 ;
- }
- int iWord = 0 ;
- //get word string .
- while( !( string->GetAt( iPos ) == ' ' || string->GetAt( iPos ) == 'r' &&
- string->GetAt( iPos + 1 ) == 'n' ) )
- {
- word[iWord++] = string->GetAt( iPos++ ) ;
- if( iPos >= iLength - 1 )
- return 2 ;
- }
- //if a word complete.
- if( string->GetAt( iPos ) == ' ' )
- {
- *pos = iPos ;
- return 1 ;
- }
- //if a line complete .
- if( string->GetAt( iPos ) == 'r' )
- {
- iPos += 2 ;
- *pos = iPos ;
- return 2 ;
- }
- return 0 ;
- }
- /***********************************************************
- ** @Description:
- ** send message string to parent window .
- **
- ** @Note:
- ** LPSTR szMsg : the message string .
- ** BOOL bCommand : is command or server reply .
- **
- ** @Return: true .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpLib::SendMsgToParentWnd(LPSTR szMsg, BOOL bCommand)
- {
- int iLength = 0 ;
- if( bCommand )
- {
- char* szCommandString ;
- memset( m_szFtpBuffer , 0 , sizeof( m_szFtpBuffer ) ) ;
- if( strncmp( szMsg , "PASS " , 5 ) == 0 )
- {
- szMsg[5] = '*' ;
- szMsg[6] = ' ' ;
- strcat( szMsg , "rn" ) ;
- }
- sprintf( m_szFtpBuffer , "COMMAND > %s" , szMsg ) ;
- int iLength = strlen( m_szFtpBuffer ) ;
- //new the buffer , this buffer will delete in parent message response function.
- szCommandString = new char[iLength + 1] ;
- strcpy( szCommandString , m_szFtpBuffer ) ;
- szCommandString[iLength] = 0 ;
- ::PostMessage( m_hWndInfo.hInfoWnd , FTP_SEND_COMMAND_MSG , 0 , (LPARAM)szCommandString ) ;
- }
- else
- {
- char* szReplyString ;
- memset( m_szFtpBuffer , 0 , sizeof( m_szFtpBuffer ) ) ;
- sprintf( m_szFtpBuffer , " %s" , szMsg ) ;
- iLength = strlen( m_szFtpBuffer ) ;
- //new the buffer , this buffer will delete in parent message response function.
- szReplyString = new char[iLength + 1] ;
- strcpy( szReplyString , m_szFtpBuffer ) ;
- szReplyString[iLength] = 0 ;
- ::PostMessage( m_hWndInfo.hInfoWnd , FTP_SERVER_REPLY_MSG , 0 , (LPARAM)szReplyString ) ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** user login ftp server.
- **
- ** @Note:
- ** SOCKET hSocket : the connect control socket handle .
- ** LPSTR szName : user name .
- ** LPSTR szPass : pass word .
- **
- ** @Return: login success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpLib::UserLogin(SOCKET hSocket, LPSTR szName, LPSTR szPass)
- {
- char szCommand[MAX_PATH] ;
- UINT nReplyCode = 0 ;
- //send user name .
- sprintf( szCommand , "USER %srn" , szName ) ;
- nReplyCode = SendFtpCommand( szCommand , hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- //send password
- sprintf( szCommand , "PASS %srn" , szPass ) ;
- nReplyCode = SendFtpCommand( szCommand , hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** anonymous login function .this function will use
- ** anonymous as user name to login ftp server.
- **
- ** @Note:
- ** SOCKET hSocket : the connect control socket handle .
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- BOOL CFtpLib::AnonymousLogin(SOCKET hSocket)
- {
- //login
- char szCommand[MAX_PATH] ;
- UINT nReplyCode = 0 ;
- sprintf( szCommand , "USER %srn" , "anonymous" ) ;
- nReplyCode = SendFtpCommand( szCommand , hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- sprintf( szCommand , "PASS %srn" , "anonymous@table.com" ) ;
- nReplyCode = SendFtpCommand( szCommand , hSocket ) ;
- if( nReplyCode >= 400 )
- return false ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- **
- ** @Note:
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- /*
- BOOL CFtpLib::SetTransmitFilePos(FTPFILEINFO *pFile , HANDLE hFile , SOCKET hSocket )
- {
- char szBuffer[MAX_PATH] ;
- sprintf( szBuffer , "SIZE %srn" , pFile->remotefilename ) ;
- //send command . and after send will check server reply .
- if( send( hSocket , szBuffer , strlen( szBuffer ) , 0 ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while Send ftp command !" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- int iLength ;
- //peek the message .
- if( ( iLength = recv( hSocket , szBuffer , 1024 , MSG_PEEK ) ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while recv server reply!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- if( iLength <= 0 )
- return (ERROR_FTP_CODE );
- int iBuffer = 0 ;
- while( szBuffer[iBuffer] != 'n' )
- {
- iBuffer ++ ;
- if( iBuffer >= iLength )
- break ;
- }
- iBuffer ++ ;
- //receive server reply string .
- if( ( iLength = recv( hSocket , szBuffer , iBuffer , 0 ) ) == SOCKET_ERROR )
- {
- int iWinsockErr = WSAGetLastError( ) ;
- sprintf( m_szFtpBuffer , " %d error while recv server reply!" , iWinsockErr ) ;
- //TRACE( NULL , m_szFtpBuffer , MSGBOX_TITLE , MB_OK ) ;
- return ( ERROR_FTP_CODE ) ;
- }
- if( iLength < 1024 )
- szBuffer[iLength] = 0 ;
- else
- szBuffer[1023] = 0 ;
- UINT nReplyCode = GetReplyCode ( szBuffer ) ;
- if( nReplyCode >= 400 )
- return false ;
- char szFileSize[MAX_PATH] ;
- __int64 iRemoteFileSize ;
- __int64 iLocalFileSize ;
- DWORD lSize ;
- DWORD hSize ;
- strcpy( szFileSize , szBuffer + 4 ) ;
- iLength = strlen( szFileSize ) ;
- szFileSize[iLength - 2 ] = ' ' ;
- iRemoteFileSize = _atoi64( szFileSize ) ;
- lSize = GetFileSize( hFile , &hSize ) ;
- iLocalFileSize = hSize ;
- iLocalFileSize = iLocalFileSize << 32 ;
- iLocalFileSize = lSize ;
- if( pFile->bfileput )
- {
- if( iRemoteFileSize > iLocalFileSize )
- {
- SendFtpCommand( "RSET 0rn" , hSocket ) ;
- return true ;
- }
- else
- {
- return true ;
- }
- }
- else
- {
- if( iRemoteFileSize < iLocalFileSize )
- {
- SendFtpCommand( "RSET 0rn" , hSocket ) ;
- return true ;
- }
- else
- {
- return true ;
- }
- }
- return true ;
- }
- */
- /***********************************************************
- ** @Description:
- ** this function is use for seek file pointer .
- **
- ** @Note:
- ** HANDLE hFile : the open file handle .
- ** __int64 distance : the move distance.
- ** DWORD MoveMethod : move method , head ,end ??
- **
- ** @Return: the file pointer moved distance
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** Date: 2001 3 26
- *************************************************************/
- __int64 CFtpLib::MyFileSeek(HANDLE hf, __int64 distance, DWORD MoveMethod)
- {
- LARGE_INTEGER li;
- li.QuadPart = distance;
- SetLastError( NO_ERROR ) ;
- //set file pointer.
- li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
- if (li.LowPart >= 0 && GetLastError() != NO_ERROR)
- {
- li.QuadPart = -1;
- }
- return li.QuadPart;
- }
- /****************************************************
- ** @Description
- ** this function is get transmit speed .
- **
- ** @Parameter
- ** DWORD dwStart : the start time.
- ** DWORD dwEnd : the end time .
- ** __int64 recData : receive data bytes.
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** @e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- int CFtpLib::GetTransmitSpeed( DWORD dwStart , DWORD dwEnd , __int64 recData)
- {
- if( dwStart >= dwEnd )
- return 0 ;
- int iSpeed = 0 ;
- iSpeed = (int) ( recData * 1000 / ( dwEnd - dwStart ) ) ;
- return iSpeed ;
- }
- /****************************************************
- ** @Description
- ** stop the command.don't let run.
- **
- ** @Parameter
- ** BOOL bRun :
- **
- ** @Return:
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CFtpLib::SetStopSign(BOOL bRun)
- {
- m_bRun = bRun ;
- }