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

📄 ftpclient.cs

📁 用C#写的FTP客户端
💻 CS
📖 第 1 页 / 共 4 页
字号:
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("LIST") ) {
				/*	125,150,250,226 (success) */
				/*	425,426,451,450,500,501,502,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 125 : case 150 : case 250 : case 226 :
						l_iRetval = 1;
						break;
					case 425 :
						m_strErrorMessage = "Error : <LIST> command. Can't open data connection";
						l_iRetval = 0;
						break;
					case 426 :
						m_strErrorMessage = "Error : <LIST> command. Connection closed; transfer aborted";
						l_iRetval = 0;
						break;
					case 451 :
						m_strErrorMessage = "Error : <LIST> command. Requested action aborted: server error in processing";
						l_iRetval = 0;
						break;
					case 450 :
						m_strErrorMessage = "Error : <LIST> command. Requested file action not taken.File unavailable (e.g., file busy)";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strErrorMessage = "Error : <LIST> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strErrorMessage = "Error : <LIST> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strErrorMessage = "Error : <LIST> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strErrorMessage = "Error : <LIST> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strErrorMessage = "Error : Unknown error in LIST command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.StartsWith("CWD") ) {
				/*	250,150 (success) */
				/*	500,501,502,530,550 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 250 : case 150 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strErrorMessage = "Error : <CWD> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strErrorMessage = "Error : <CWD> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strErrorMessage = "Error : <CWD> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strErrorMessage = "Error : <CWD> command. User not logged on";
						l_iRetval = 0;
						break;
					case 550 :
						m_strErrorMessage = "Error : <CWD> command. Requested action not taken.File unavailable (e.g., file not found, no access).";
						l_iRetval = 0;
						break;
					default:
						m_strErrorMessage = "Error : Unknown error in CWD command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("RETR") ) {
				/*	125,150,110,250,226 (success) */
				/*	226,425,426,451,450,500,501,502,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 125 : case 150 : case 110 : case 250 : case 226 :
						l_iRetval = 1;
						break;
					case 425 :
						m_strErrorMessage = "Error : <RETR> command. Can't open data connection";
						l_iRetval = 0;
						break;
					case 426 :
						m_strErrorMessage = "Error : <RETR> command. Connection closed; transfer aborted";
						l_iRetval = 0;
						break;
					case 451 :
						m_strErrorMessage = "Error : <RETR> command. Requested action aborted: server error in processing";
						l_iRetval = 0;
						break;
					case 450 :
						m_strErrorMessage = "Error : <RETR> command. Requested file action not taken.File unavailable (e.g., file busy)";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strErrorMessage = "Error : <RETR> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strErrorMessage = "Error : <RETR> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strErrorMessage = "Error : <RETR> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strErrorMessage = "Error : <RETR> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strErrorMessage = "Error : Unknown error in RETR command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("QUIT") ) {
				/*	221	(success) */
				/*	500 */
				switch( l_iResponseCode ) {
					case 221 :
						l_iRetval = 1;
						break;
					case 500 :
						m_strErrorMessage = "Error : Syntax error, command unrecognized";
						l_iRetval = 1;
						break;
					default :
						m_strErrorMessage = "Error : Unknown error in QUIT command. Response code :" +  l_iResponseCode;
						l_iRetval = 1;
						break;
				}
			}
			return l_iRetval;
		}

		public int ChangeWorkingDirectory(string l_strDirectory){
			/*	CWD command */
			string l_strCommand = "";
			string l_strOutput = "";
			int l_iRetval = 0;

			m_strErrorMessage = "";

			/*	USER command */
			l_strCommand = "CWD " + l_strDirectory + "\r\n";
			l_iRetval = ExecuteCommand(l_strCommand,ref l_strOutput);
			if ( l_iRetval == 0 ) {
				return l_iRetval;
			}

			m_strCurrentWorkingDir = l_strDirectory;
			return 1;
		}

		/*	Get Current Working Directory on the Server */
		public string GetCurrentWorkingDirectory(){
			return m_strCurrentWorkingDir;
		}

		public int GetServerOS(){
			return m_iServerOS;			
		}

		/*	Set Transfer type
		 *	'A' for ASCII
		 *	'B' for Binary
		 * */
		public int SetTransferType(char l_chType){
			/*	TYPE command, Set Type = ASCII */
			m_strErrorMessage = "";
			if ( l_chType != 'A' && l_chType != 'B' ) {
				m_strErrorMessage = "Invalid Transfer type passed '" + l_chType +"'";
				return 0;
			}
			string l_strMessage = "",l_strCommand = "", l_strOutput = "";
			/*	Set Message in the parent window */
			if ( l_chType == 'A' ) {
				l_strMessage = "Setting to Ascii Type...";
			}
			else if (l_chType == 'B' ) {
				l_strMessage = "Setting to Binary Type...";
			}

			m_ParentWindow.SetStausMessage(l_strMessage);

			l_strCommand = "TYPE " + l_chType + "\r\n";
			int l_iRetval = ExecuteCommand(l_strCommand,ref l_strOutput);
			if ( l_iRetval == 0 ) {
				return 0;
			}
			return 1;
		}

		/*	Download file from FTP Server */
		public int DownLoadFile(DownloadFileData l_FileObject,char l_chTransferType){

			/*	Read 5 K at a time	*/
			Byte[] l_bRecvData = new Byte[5120];
			TCPListener l_FTPListener = null;
			Socket l_ClientDataSocket = null;
			bool l_bAcceptFailed = false;

			/*	Local file handle for stream mode */
			FileStream l_fsOutFile = null;

			/*	File handle for Binary files */
			BinaryWriter l_fbOutFile = null;

			int l_iRetval = 0, l_iDataPort = 0;
			string l_strServerFileName = l_FileObject.ServerFileName ;
			string l_strLocalFileName = l_FileObject.LocalFileName;
			int l_iFileSize = l_FileObject.FileSize;
			int l_iBytesRevd = 0, l_iBufferLimit = 5119;

			try {
				l_iDataPort = GetPort();
				l_FTPListener = new TCPListener(l_iDataPort);
				l_FTPListener.Start();
				string l_strOutput = "",l_strCommand = "";
				string l_strPortParams = GetPortParameters(l_iDataPort);

				/*	Open Files here */
				if ( l_chTransferType == 'A' ) {
					/*	Open ASCII stream */
					l_fsOutFile = new FileStream(l_strLocalFileName,FileMode.Create,FileAccess.Write);
				}

				/*	PORT command */
				string l_strPortCommand = "PORT " + l_strPortParams + "\r\n";
				l_iRetval = ExecuteCommand(l_strPortCommand,ref l_strOutput);
				if ( l_iRetval == 0 ) {
					return l_iRetval;
				}

				l_strCommand = "RETR " + l_strServerFileName + "\r\n";
				/*	Execute the actual command here */
				l_iRetval = ExecuteCommand(l_strCommand,ref l_strOutput);
				if ( l_iRetval == 0 ) {
					return l_iRetval;
				}

				/*	Accept Client connection here,
				 *	only if there is a pending connection
				 *  */
				if ( l_FTPListener.Pending() ) {
					l_ClientDataSocket = l_FTPListener.Accept();
				}
				else {
					/*	Else return from the download function with error */
					l_bAcceptFailed = true;
					m_strErrorMessage = "Error : No connection from Server. Aborting the process. Try later";
					return 0;
				}
				
				l_ClientDataSocket.SetSockOpt(SocketOption.SolSocket,SocketOption.SoRcvTimeo,m_iRecvTimeout);
				
				l_bRecvData.Initialize();
				l_strOutput = "";
				l_iRetval = 0;

				/*	Loop through and Receive all the data */
				
				int l_iTimes = 1;
				//for ( ;  l_ClientDataSocket.Available > 0  ; ) {
				for ( ; (l_iRetval = l_ClientDataSocket.Receive(l_bRecvData,l_iBufferLimit,0)) > 0  ; ) {
					l_bRecvData.Initialize();
					//l_iRetval = l_ClientDataSocket.Receive(l_bRecvData,l_iBufferLimit,0);
					/*	Write to Local file here */
					l_fsOutFile.Write(l_bRecvData,0,l_iRetval);
					l_fsOutFile.Flush();

					/*	Update the Progress bar control */
					l_iBytesRevd += l_iRetval;
					if ( l_iBytesRevd <= ( l_iBufferLimit * l_iTimes) ) {
						m_ParentWindow.m_progressBarFileStatus.PerformStep();
						m_ParentWindow.m_progressBarFileStatus.Refresh();
						/*	Once in a while refresh main window */
						if ( l_iTimes % 10 == 0 ) {
							m_ParentWindow.Refresh();
						}
						l_iTimes++;
					}
				}

				if ( l_iRetval == 0 ) {
					/*	Connection closed by FTP Server process 
					 *	then the file transfer is complete
					 * */
					l_iRetval = 1;
				}
				else if ( l_iRetval == -1 ) {
					/*	If Timeout occured then the FTP Server process
					 *	didn't respond in a timely fashion
					 */
					l_iRetval = 0;
					m_strErrorMessage = "Error : Timeout occured. FTP Server process did not respond in a timely fashion. Try increasing Receive timeout property in Configure dialog";
				}

				/*	Close Data Connection here */
				if ( l_ClientDataSocket != null ) {
					l_ClientDataSocket.Shutdown(SocketShutdown.SdBoth);
					l_ClientDataSocket.Close();
					l_ClientDataSocket = null;
				}

				/*	Code to receive the response code 226 for 
				 *	RETR command from command socket
				 * */
				Byte[] l_bRecvCommData = new Byte[512];
				l_bRecvCommData.Initialize();
				string l_strTemp = "";

				l_strOutput = "";
				int l_iRetval1 = 1;
				if ( m_strErrorMessage.Length > 0 ) {
					l_strTemp = m_strErrorMessage;
				}

				for ( ; m_ClientSocket.Available > 0 ; ) {
					l_iRetval1 = m_ClientSocket.Receive(l_bRecvCommData,511,0);
					l_strTemp = Encoding.ASCII.GetString(l_bRecvCommData,0,l_iRetval1);
					l_strOutput += l_strTemp;
				}

				/*	Parse the output string */
				if ( l_strOutput.Length > 3 ) {
					l_iRetval1 = ParseResponseCode("RETR",l_strOutput.Substring(0,3));
				}
				if ( l_iRetval1 == 0 ) {
					if ( l_strTemp.Length > 0 ) {
						m_strErrorMessage += l_strTemp;
					}
					l_iRetval = 0;
				}
				return l_iRetval;
			}
			catch ( Exception e ) {
				m_strErrorMessage = "Error : " + e;
				return 0;
			}
			finally{
				if ( l_FTPListener != null ) {
					l_FTPListener.Stop();
					l_FTPListener = null;
				}
				if ( l_ClientDataSocket != null ) {
					l_ClientDataSocket.Shutdown(SocketShutdown.SdBoth);
					l_ClientDataSocket.Close();
					l_ClientDataSocket = null;
				}
				if ( l_fsOutFile != null ) {
					l_fsOutFile.Close();
				}
				if ( l_fbOutFile != null ) {
					l_fbOutFile.Close();
				}

				/*	If Accept() failed, then receive the data from
				 *	command connection and ignore it.
				 * */
				if ( l_bAcceptFailed == true ) {
					Byte[] l_bRecvCommData = new Byte[512];
					l_bRecvCommData.Initialize();

					/*	Get all data and do not do anything with the data.
					 *	This is just to clear the command connection data
					 */
					for ( ; m_ClientSocket.Available > 0 ; ) {
						m_ClientSocket.Receive(l_bRecvCommData,511,0);
					}
				}
			}	/*	End of finally */
		}	/*	End of download file */
	}
}

⌨️ 快捷键说明

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