📄 ftpsockclient.cpp
字号:
// FtpSockClient.cpp: implementation of the CFtpSockClient class.
//
//////////////////////////////////////////////////////////////////////
#include "FtpSockClient.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define RES_CODE_SEGMENT 3
#define SERVER_READY 220
#define USER_OK 331
#define PASS_OK 230
#define DIR_OK 257
#define CHDIR_OK 250
#define DELDIR_OK 250
#define MOVE_OK 350
#define PORT_OK 200
#define DATA_OK 150
#define GET_OK 226
#define SESSION_OVER 221
//#define MOVE_DONE 350
#define POS_HYPHEN 3
#define CRLF "\r\n"
#define DIR_SEPERATOR "/"
#define BLANK ""
#define MAX_BUFFER 256
#define ZERO 0
#define ASCII "A"
#define IMAGE "I"
#define ERR_SOCK_LOOKUP 5000
#define ERR_SOCK_CREATE 5001
#define ERR_SOCK_LISTEN 5002
#define ERR_SOCK_PORT 5003
#define ERR_SOCK_ACCEPT 5004
#define ERR_SOCK_LOCFILE 5004
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFtpSockClient::CFtpSockClient( )
:m_pCtrlSock( NULL ),
m_bSession( FALSE ),
m_csHost( _T("") ),
m_csTransferMode(_T("A"))
{
}
CFtpSockClient::~CFtpSockClient()
{
CloseChannel();
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::CreateChannel()
{
ASSERT( m_pCtrlSock == NULL );
// allocate socket from heap
if( ! ( m_pCtrlSock = new CSocket ) )
return FALSE;
if( ! m_pCtrlSock -> Create())
{
SetErrorCode( ERR_SOCK_CREATE );
return FALSE;
}
// The memory was allocated fine and the create attempt was successfull
return TRUE;
}
/*====================================================================================================*/
/*====================================================================================================*/
void CFtpSockClient::CloseChannel()
{
if( m_pCtrlSock != NULL )
{
delete m_pCtrlSock;
m_pCtrlSock = NULL;
}
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::ConnectToServer( CString csServer, UINT unPort )
{
// First create the socket.
CreateChannel();
ASSERT ( m_pCtrlSock != NULL );
ASSERT ( csServer.GetLength() > 0 );
if( m_pCtrlSock == NULL )
{
TRACE(_T("\nCode Message - Control socket has not been created ! \r\n") );
return FALSE;
}
m_bSession = m_pCtrlSock -> Connect( csServer, unPort );
// This while debugging will trace the output in the ouput window
int nErrorCode = GetLastError();
TraceErrorCode( nErrorCode );
CString csWelcome = GetResponse();
if( GetErrorCode() != SERVER_READY )
m_bSession = FALSE;
return m_bSession;
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::DisconnectFromServer()
{
ASSERT( m_pCtrlSock != NULL );
if( m_pCtrlSock == NULL )
{
TRACE(_T("\nCode Message - Control socket has not been created ! \r\n") );
return FALSE;
}
ASSERT ( m_bSession );
if( ! m_bSession )
{
TRACE(_T("\nCode Message - No Session Established"));
return TRUE;
}
BOOL bRetVal = SendCommand( L"QUIT" );
GetResponse();
if( !bRetVal || GetErrorCode( ) != SESSION_OVER )
{
TRACE(_T("\nServer Message - Could not disconnect session from server \r\n"));
return FALSE;
}
CloseChannel();
return TRUE;
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::UserLogin( CString csUserName, CString csPassword )
{
CString csCommand = "USER " + csUserName;
BOOL bRetVal = SendCommand( csCommand );
GetResponse();
if( !bRetVal || GetErrorCode( ) != USER_OK )
{
TRACE(_T("\nServer Message - User access not allowed. \r\n"));
return FALSE;
}
csCommand = "PASS " + csPassword;
bRetVal = SendCommand( csCommand );
GetResponse();
if( ! bRetVal || GetErrorCode( ) != PASS_OK )
{
TRACE(_T("\nServer Message - User access not allowed. \r\n"));
return FALSE;
}
TRACE(_T("\nServer Message - User logged in. \r\n"));
return TRUE;
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::SendCommand( CString csCommand )
{
ASSERT ( m_pCtrlSock != NULL );
if( m_pCtrlSock == NULL )
{
TRACE(_T("\nCode Message - Socket not initialized"));
return TRUE;
}
ASSERT ( m_bSession );
if( ! m_bSession )
{
TRACE(_T("\nCode Message - No Session Established"));
return TRUE;
}
if( csCommand.IsEmpty() )
return FALSE;
if( csCommand.Right(2) != CRLF )
csCommand += CRLF;
UINT nLen = csCommand.GetLength();
char *pBuffer = new char[ nLen + 1 ];
WideCharToMultiByte ( CP_ACP, 0, csCommand.GetBuffer( csCommand.GetLength() ), -1, pBuffer, nLen + 1, NULL, NULL );
UINT nSent = 0;
do
{
nSent += m_pCtrlSock ->Send( pBuffer, nLen );
if( nSent == 0 )
{
if( pBuffer != NULL )
{
delete [] pBuffer;
pBuffer = NULL;
}
return FALSE;
}
}while ( nSent < nLen );
csCommand.ReleaseBuffer();
if( pBuffer != NULL )
{
delete [] pBuffer;
pBuffer = NULL;
}
return TRUE;
}
/*====================================================================================================*/
/*====================================================================================================*/
CString CFtpSockClient::GetResponse( )
{
ASSERT ( m_pCtrlSock != NULL );
if( m_pCtrlSock == NULL )
{
TRACE(_T("\nCode Message - Socket not initialized"));
return TRUE;
}
ASSERT ( m_bSession );
if( ! m_bSession )
{
TRACE(_T("\nCode Message - No Session Established"));
return TRUE;
}
CString csResp = "";
CString csCompleteString = _T("");
char ch;
BOOL bCheckMultiLine = TRUE;
while(bCheckMultiLine)
{
while( ( csResp.Right(2) != CRLF ) )
{
int nRead = m_pCtrlSock -> Receive( &ch, 1, 0 );
csResp += ch;
}
csCompleteString += csResp;
if( csResp[POS_HYPHEN] == ' ' )
bCheckMultiLine = FALSE;
csResp = _T("");
}
CString csTemp = csCompleteString.Left( RES_CODE_SEGMENT );
int nLen = csTemp.GetLength();
char pBuffer[ 4 ];
WideCharToMultiByte ( CP_ACP, 0, csTemp.GetBuffer( csTemp.GetLength() ), -1, pBuffer, nLen + 1, NULL, NULL );
SetErrorCode( atoi( pBuffer ) );
csTemp.ReleaseBuffer();
return m_csStatusString = csCompleteString;
}
/*====================================================================================================*/
/*====================================================================================================*/
void CFtpSockClient::TraceErrorCode(int nErrorCode)
{
if (0 != nErrorCode)
{
switch( nErrorCode )
{
case WSAEADDRINUSE:
TRACE(_T("\nCode Message - The specified address is already in use. \r\n") );
break;
case WSAEADDRNOTAVAIL:
TRACE(_T("\nCode Message - The specified address is not available from the local machine. \r\n") );
break;
case WSAEAFNOSUPPORT:
TRACE(_T("\nCode Message - Addresses in the specified family cannot be used with this socket. \r\n") );
break;
case WSAECONNREFUSED:
TRACE(_T("\nCode Message - The attempt to connect was forcefully rejected. \r\n") );
break;
case WSAEDESTADDRREQ:
TRACE(_T("\nCode Message - A destination address is required. \r\n") );
break;
case WSAEFAULT:
TRACE(_T("\nCode Message - The lpSockAddrLen argument is incorrect. \r\n") );
break;
case WSAEINVAL:
TRACE(_T("\nCode Message - The socket is already bound to an address. \r\n") );
break;
case WSAEISCONN:
TRACE(_T("\nCode Message - The socket is already connected. \r\n") );
break;
case WSAEMFILE:
TRACE(_T("\nCode Message - No more file descriptors are available. \r\n") );
break;
case WSAENETUNREACH:
TRACE(_T("\nCode Message - The network cannot be reached from this host at this time. \r\n") );
break;
case WSAENOBUFS:
TRACE(_T("\nCode Message - No buffer space is available. The socket cannot be connected. \r\n") );
break;
case WSAENOTCONN:
TRACE(_T("\nCode Message - The socket is not connected. \r\n") );
break;
case WSAENOTSOCK:
TRACE(_T("\nCode Message - The descriptor is a file, not a socket. \r\n") );
break;
case WSAETIMEDOUT:
TRACE(_T("\nCode Message - The attempt to connect timed out without establishing a connection. \r\n") );
break;
case WSAECONNABORTED:
TRACE(_T("\nCode Message - The virtual circuit was aborted due to timeout or other failure. \r\n") );
break;
default:
TCHAR szError[256];
wsprintf(szError, L"Code Message - Connect error: %d", nErrorCode);
TRACE(szError);
break;
}
}
else
TRACE(_T("\nCode Message - Connect attempt Successful") );
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::AssertFailure()
{
return FALSE;
}
/*====================================================================================================*/
/*====================================================================================================*/
CString CFtpSockClient::GetWorkingDirectory( )
{
CString csCommand = "PWD";
BOOL bRetVal = SendCommand( csCommand );
CString csDir = GetResponse();
if( bRetVal && GetErrorCode() == DIR_OK )
{
int nFound = csDir.Find(L"\"");
if( nFound != -1 )
{
csDir = csDir.Right( csDir.GetLength() - nFound -1 );
nFound = csDir.Find( L"\"" );
csDir = csDir.Left( nFound );
return csDir;
}
return BLANK;
}
return BLANK;
}
/*====================================================================================================*/
/*====================================================================================================*/
BOOL CFtpSockClient::SetWorkingDirectory( CString csDirectory )
{
ASSERT( csDirectory != BLANK );
if( csDirectory == BLANK )
return FALSE;
CString csCommand = "CWD " + csDirectory;
BOOL bRetVal = SendCommand( csCommand );
GetResponse();
if( bRetVal && GetErrorCode() == CHDIR_OK )
return TRUE;
return FALSE;
}
/*====================================================================================================*/
/*====================================================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -