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

📄 ftpsockclient.cpp

📁 ce ftp客户端
💻 CPP
📖 第 1 页 / 共 2 页
字号:
BOOL CFtpSockClient::ChangeWorkingDirectory( CString csDirectory )
{
	return SetWorkingDirectory( csDirectory );
}

/*====================================================================================================*/	
/*====================================================================================================*/	

BOOL CFtpSockClient::MakeDirectory( CString csDirectory )
{
	if( csDirectory == BLANK )
		csDirectory = "New Folder";
	CString csNewDir = GetWorkingDirectory();

	if( csNewDir.Right(1) != DIR_SEPERATOR )
		csNewDir += DIR_SEPERATOR;

	CString csCommand = "MKD ";
	csCommand += csDirectory;

	BOOL bRetVal = SendCommand( csCommand );
	
	GetResponse();

	if( bRetVal && GetErrorCode() == DIR_OK )
		return TRUE;

	return FALSE;
}

/*====================================================================================================*/	
/*====================================================================================================*/	

BOOL CFtpSockClient::RemoveDirectory( CString csDirectory )
{
	ASSERT( csDirectory != BLANK );
	
	if( csDirectory == BLANK )
		return FALSE;
	
	CString csCommand = "RMD ";
	csCommand += csDirectory;
	
	BOOL bRetVal = SendCommand( csCommand );
	
	GetResponse();

	if( bRetVal && GetErrorCode() == DELDIR_OK )
		return TRUE;

	return FALSE;
}

/*====================================================================================================*/	
/*====================================================================================================*/	

void CFtpSockClient::SetErrorCode( int nErrorCode ) 
{
	m_nLastError = nErrorCode;
}

/*====================================================================================================*/	
/*====================================================================================================*/	

int CFtpSockClient::GetErrorCode() 
{
	return m_nLastError;
}

CString CFtpSockClient::GetHELP()
{
	SendCommand( "HELP" );
	m_csHelpString = GetResponse();
	return m_csHelpString;
}


BOOL CFtpSockClient::GetFile( CString csRemoteFile, CString csLocalFile )
{
	if( !OpenDataChannel() )
		return FALSE;

	CString csCommand = "RETR " + csRemoteFile;
	BOOL bRetVal = SendCommand( csCommand );
	GetResponse();
	if( !bRetVal || GetErrorCode() != DATA_OK )
	{
		m_SockSrvr.Close();
		return FALSE;
	}

	CAsyncSocket	DataSocket;
	// accept inbound data connection from server
	if( ! m_SockSrvr.Accept( DataSocket ) )
	{
		SetErrorCode ( ERR_SOCK_ACCEPT );
		DataSocket.Close();
		m_SockSrvr.Close();
		return FALSE; 
	}

	CFile	cfDataFile;

	if( ! cfDataFile.Open( csLocalFile, CFile::modeCreate | CFile::modeWrite ) )
	{
		SetErrorCode( ERR_SOCK_LOCFILE );
		return FALSE;
	}

	char szBuffer[ MAX_BUFFER ];
	int nNumBytesRead;
	CString csLocFileBuffer;
	while ( TRUE )
	{
		memset( szBuffer, '\0', MAX_BUFFER );

		nNumBytesRead = DataSocket.Receive( szBuffer, MAX_BUFFER - 1 );
					
		if( ! nNumBytesRead || nNumBytesRead == SOCKET_ERROR ) 
			break; // ( EOF || network error )

		csLocFileBuffer += szBuffer;
	}

	cfDataFile.Write( csLocFileBuffer, csLocFileBuffer.GetLength() );

	DataSocket.Close();
	
	m_SockSrvr.Close();

	cfDataFile.Close();

	GetResponse();

	if( GetErrorCode() != GET_OK )
		return FALSE;

	return TRUE;
}

BOOL CFtpSockClient::PutFile( CString csLocalFile, CString csRemoteFile )
{

	if( !OpenDataChannel() )
		return FALSE;

	CString csCommand = "STOR " + csRemoteFile;

	BOOL bRetVal = SendCommand( csCommand );
	GetResponse();
	if( !bRetVal || GetErrorCode() != DATA_OK )
	{
		m_SockSrvr.Close();
		return FALSE;
	}

	CAsyncSocket	DataSocket;
	// accept inbound data connection from server
	if( ! m_SockSrvr.Accept( DataSocket ) )
	{
		SetErrorCode ( ERR_SOCK_ACCEPT );
		DataSocket.Close();
		m_SockSrvr.Close();
		return FALSE; 
	}

	CFile	cfDataFile;

	if( ! cfDataFile.Open( csLocalFile, CFile::modeRead | CFile::shareDenyNone ) )
	{
		SetErrorCode( ERR_SOCK_LOCFILE );
		return FALSE;
	}

	char szBuffer[ MAX_BUFFER ];
	int nNumBytesRead;
	int nNumBytesSent;
	CString csLocFileBuffer;
	while ( TRUE )
	{
		memset( szBuffer,'\0', MAX_BUFFER );

		nNumBytesRead = cfDataFile.Read( szBuffer, MAX_BUFFER );
		
		if(! nNumBytesRead ) //EOF
			break; 
		
		if( ( nNumBytesSent = DataSocket.Send( szBuffer, nNumBytesRead, 0 )) == SOCKET_ERROR ) 
			break;
		
		// if we sent fewer bytes than we read from file, rewind file pointer
		if( nNumBytesRead != nNumBytesSent ) 
			cfDataFile.Seek( nNumBytesSent - nNumBytesRead, CFile::current );
	}

	DataSocket.Close();
	
	m_SockSrvr.Close();

	cfDataFile.Close();

	GetResponse();

	if( GetErrorCode() != GET_OK )
		return FALSE;

	return TRUE;
}

BOOL CFtpSockClient::MoveFile( CString csSourcePath, CString csDestPath )
{
	ASSERT( csSourcePath != BLANK );
	ASSERT( csDestPath != BLANK );	
	if( csSourcePath == BLANK || csDestPath == BLANK )
		return FALSE;
	
	CString csCommand = "RNFR " + csSourcePath;
	BOOL bRetVal = SendCommand( csCommand );
	GetResponse();
	if( bRetVal && GetErrorCode() == MOVE_OK )
	{
		csCommand = "RNTO " + csDestPath;
		bRetVal = SendCommand( csCommand );
		GetResponse();
//		if( bRetVal && GetErrorCode() == MOVE_DONE )
//		{
//		}
		return TRUE;
	}
	
	return FALSE;
}





BOOL CFtpSockClient::OpenDataChannel()
{
	CString csLocHost;
	unsigned int nLocPort;

	// get the local IP address off the control channel socket
	if( ! m_pCtrlSock -> GetSockName( csLocHost, nLocPort ) ) 
	{
		
		return FALSE;
	}
	
	int nFound;
	// convert returned '.' in ip address to ','
	while( 1 ) 
	{
		if( ( nFound = csLocHost.Find( L"." ) ) == -1 ) 
			break;
		csLocHost.SetAt( nFound, ',' );
	}

	// create listen socket (let MFC choose the port) & start the socket listening
	if( ! m_SockSrvr.Create ( 0, SOCK_STREAM, NULL )  )
	{
		SetErrorCode( ERR_SOCK_CREATE );
		return FALSE;
	}

	if( ! m_SockSrvr.Listen ( ) ) 
	{
		SetErrorCode( ERR_SOCK_LISTEN );
		m_SockSrvr.Close();
		return FALSE;
	}

	CString csTempAddr;
	// get the port that MFC chose
	if( ! m_SockSrvr.GetSockName( csTempAddr, nLocPort ) ) 
	{
		SetErrorCode( ERR_SOCK_LOOKUP );
		m_SockSrvr.Close();
		return FALSE;
	}

	// set Transfer Type
	BOOL bRetVal = SendCommand( m_csTransferMode );

	GetResponse();


	// convert the port number to 2 bytes + add to the local IP
	csLocHost.Format( csLocHost + ",%d,%d",nLocPort / 256 , nLocPort % 256 );

	// Send Port Command
	bRetVal = SendCommand ( "PORT " + csLocHost );
	GetResponse();
	if( !bRetVal || GetErrorCode() != PORT_OK )
	{
		SetErrorCode( ERR_SOCK_PORT );
		m_SockSrvr.Close();
		return FALSE;
	}

	return TRUE;
}


BOOL CFtpSockClient::SetTransferModeAscii( )
{
	m_csTransferMode = ASCII;
	return TRUE;
}

BOOL CFtpSockClient::SetTransferModeImage()
{
	m_csTransferMode = IMAGE;
	return TRUE;
}


//		The attributes to be set sholu be like 777, 040, - UNIX chmod options actually
BOOL CFtpSockClient::ChangeFileAttrib( CString csFilePath, CString csAttrib )
{
	ASSERT( csFilePath != BLANK );
	ASSERT( csAttrib != BLANK );

	if( csFilePath == BLANK || csAttrib == BLANK )
		return FALSE;
	
	CString csCommand = "SITE CHMOD ";
	csCommand += csFilePath;

	if( ! SendCommand( csCommand ) )
		return FALSE;
	
	GetResponse();

	return TRUE;
}

BOOL CFtpSockClient::ChangeUser( CString csUserName, CString csPassword )
{
	ASSERT( csUserName != BLANK );
	ASSERT( csPassword != BLANK );

	if( csUserName == BLANK || csPassword == BLANK )
		return FALSE;

	CString csCommand = "USER " + csUserName;
	
	BOOL bRetVal = SendCommand( csCommand );
	GetResponse();

	if( !bRetVal || GetErrorCode() != USER_OK )
		return FALSE;
	
	csCommand = "PASS " + csPassword;
	
	bRetVal = SendCommand( csCommand );
	GetResponse();

	if( !bRetVal || GetErrorCode() != PASS_OK )
		return FALSE;
	return TRUE;
}

UINT CFtpSockClient::GetFileList( CString csDirectory, CString& csFileList )
{
	if( !OpenDataChannel() )
		return ZERO;

	CString csCommand = "LIST -L";

	BOOL bRetVal = SendCommand( csCommand );
	
	GetResponse();
	if( !bRetVal || GetErrorCode() != DATA_OK )
	{
		m_SockSrvr.Close();
		return ZERO;
	}

	CAsyncSocket	DataSocket;
	// accept inbound data connection from server
	if( ! m_SockSrvr.Accept( DataSocket ) )
	{
		SetErrorCode ( ERR_SOCK_ACCEPT );
		DataSocket.Close();
		m_SockSrvr.Close();
		return ZERO;
	}


	char szBuffer[ MAX_BUFFER ];
	int nNumBytesRead;

	while ( TRUE )
	{
		memset( szBuffer, '\0', MAX_BUFFER );

		nNumBytesRead = DataSocket.Receive( szBuffer, MAX_BUFFER - 1 );
					
		if( ! nNumBytesRead || nNumBytesRead == SOCKET_ERROR ) 
			break; // ( EOF || network error )

		m_FileList += szBuffer;
	}

	csFileList = m_FileList;
	DataSocket.Close();
	
	m_SockSrvr.Close();

	GetResponse();

	if( GetErrorCode() != GET_OK )
		return ZERO;

	return 10;
}

CString CFtpSockClient::GetStatusFromServer()
{
	return m_csStatusString;
}

⌨️ 快捷键说明

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