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

📄 paulsftpmodel.cs

📁 C#的FTP客户端演示例子
💻 CS
📖 第 1 页 / 共 2 页
字号:

		}

		// FTP Command RETR
		public void commandRETR () 
		{      
			string pServerFile        = ftpViewState.serverFile;
			string pClientDestination = ftpViewState.clientFile;
			Socket dataSocket;
			// Now open a data socket
			dataSocket  = getDataSocket();
			string  command = "TYPE I"; // We want a binary type
			this.handleServerOutput ("LOCAL> " + command);
			command += CRLF;
			commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
			commandReply  reply = this.readCommandReply();
  
			command = "RETR " + pServerFile; //
			this.handleServerOutput ("LOCAL> " + command);
			command += CRLF;
			commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
			reply = this.readCommandReply();
  
			// Now read data from the dataSocket
			Byte[] readBytes;
			ArrayList byteArray = new ArrayList();
			int sizeReceived;
			int totalBytes = 0;
			string serverMessage = "";
  
			FileStream oFileStream = new FileStream(pClientDestination , FileMode.Create);
  
			do 
			{
				readBytes = new Byte[DATA_BLOCK_SIZE];
				sizeReceived  = dataSocket.Receive(readBytes, readBytes.Length, SocketFlags.None);
				serverMessage  += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
				oFileStream.Write(readBytes,0, sizeReceived);
        
				// Update percentage complete signal
				totalBytes = totalBytes + sizeReceived;
				this.fileDownloadPercent = Convert.ToByte((totalBytes * 100) / 1467536) ;

				//        this.fileDownloadPercent = new Random().Next(100);


			}
			while (sizeReceived == readBytes.Length ); // Go back for more if necessary
  
			//        Clipboard.SetDataObject(totalBytes);
			this.fileDownloadPercent = 100;
     
			oFileStream.Close();
      
			reply = this.readCommandReply();
			this.pushChanges();
		}
  
		// FTP Command STOR
		public void commandSTOR () 
		{
			Socket dataSocket;
			// Now open a data socket
			dataSocket  = getDataSocket();
			string  command = "TYPE I"; // We want a binary type
			this.handleServerOutput("LOCAL> " + command);
			this.handleServerOutput (command);
			command += CRLF;
			commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
			commandReply  reply = this.readCommandReply();
  
			command = "STOR " + this.ftpViewState.serverFile; //
			this.handleServerOutput ("LOCAL> " + command);
			command += CRLF;
			commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
			reply = this.readCommandReply();
  
			// Now send data from the dataSocket
			ArrayList byteArray = new ArrayList();
			int sizeReceived;
  
			FileStream fileToRead = new FileStream(this.ftpViewState.clientFile, FileMode.Open);
			byte[] buffer = new byte[DATA_BLOCK_SIZE];
  
			int bytesRead;
			do 
			{
				bytesRead = fileToRead.Read(buffer,0, DATA_BLOCK_SIZE);
				sizeReceived  = dataSocket.Send(buffer, bytesRead, SocketFlags.None);
				System.Console.Write ("."); // Make this a % indicator eventually
			}
			while (bytesRead == buffer.Length ); // Go back for more if necessary
     
			// Close the dile and data connection
			fileToRead.Close();
			dataSocket.Close();
     
			reply = this.readCommandReply();
			this.pushChanges();

			// clear the cache for the current directory and LIST
			ftpViewState.flushCurrentDirectoryCache();
			this.commandLIST();
			this.pushChanges();
		}

		// ///////////////////////////////////////////////////
		// /////////////// PRIVATE METHODS ///////////////////
		// ///////////////////////////////////////////////////
  
		// This method could easily be changed to a different implementation (e.g. Write to console)
		private void handleServerOutput (string pServerOutput) 
		{
			this.messagesFromServer.Enqueue(pServerOutput);
			//      System.Console.WriteLine(this.messagesFromServer.Dequeue());
		}

		private Socket getDataSocket () 
		{
			Socket localSocket;
      
			if (!this.ftpViewState.connected) Console.WriteLine ("******* NOT connected - an error will occur");
  
			/* REWRITE TO USE PORT when possible
			string  portDetails;
			IPHostEntry clientIPHostEntry = Dns.Resolve(Dns.GetHostName());
			string address = clientIPHostEntry.AddressList[0].ToString();
			*/
  
			string  command = "PASV";
			this.handleServerOutput("LOCAL> " + command);
			command += CRLF;
			commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
			commandReply  reply = this.readCommandReply();
  
			// Parse out the host and port address
			string fullAddress = reply.message;
			fullAddress = fullAddress.Remove(0, fullAddress.IndexOf('(') + 1);
			fullAddress = fullAddress.Substring(0, fullAddress.IndexOf(')'));
			string[] addressParts = fullAddress.Split(',');
			string pasvAddress = addressParts[0] + "." + addressParts[1] + "." + addressParts[2] + "." + addressParts[3];
			int    pasvPort    = Convert.ToInt32(addressParts[4]) * 256 + Convert.ToInt32(addressParts[5]);
  
			// Open the Data socket
			localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			localSocket.Connect(new IPEndPoint(IPAddress.Parse(pasvAddress), pasvPort));
  
			return localSocket;
		}
    
		private commandReply readCommandReply() 
		{
			// Read the first line
			commandReply  reply;
			Byte[] readBytes;
			bool    foundLastLine;
			int sizeReceived;
			string serverMessage = "";

			//int count = 0;

			if (this.commandSocket.Poll(1000, SelectMode.SelectRead)==true );
		{     
     
			do 
			{
				readBytes = new Byte[COMMAND_BLOCK_SIZE];
				sizeReceived  = this.commandSocket.Receive(readBytes);
				serverMessage  += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
				//		 count++;
			}
			while (sizeReceived == readBytes.Length & !serverMessage.EndsWith(CRLF)); // Go back for more if necessary

			//& count < 20 

			// Check id there are more lines to receive
			reply.code    = Convert.ToInt32(serverMessage.Substring(0,3));
			if (serverMessage[3] == '-' & serverMessage.IndexOf(CRLF + reply.code + ' ') == -1) 
			{
				//		reply.code    = Convert.ToInt32(serverMessage.Substring(0,3));
				do 
				{
					readBytes = new Byte[COMMAND_BLOCK_SIZE];
					sizeReceived  = this.commandSocket.Receive(readBytes);
					serverMessage  += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
					foundLastLine = serverMessage.IndexOf(CRLF + reply.code + ' ') > 0;
				}
				while (!foundLastLine | !serverMessage.EndsWith(CRLF)); // Go back for more if necessary
			}

			// Extract the code and message
			reply.code    = Convert.ToInt32(serverMessage.Substring(0,3));
			reply.message = serverMessage.Substring(4,serverMessage.Length-6); // Ignore the <CRLF>
			this.handleServerOutput(reply.code + " " + reply.message);
		}    
			return reply;
		}
  
	}
}



/*
    public void commandBlockRETR () {
      Socket dataSocket;
      // Now open a data socket
      dataSocket  = getDataSocket();
      string  command = "TYPE I"; // We want a binary type
      this.handleServerOutput ("LOCAL> " + command);
      command += CRLF;
      commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
      commandReply  reply = this.readCommandReply();
  
      // Request BLOCK mode
      command = "MODE S"; // We want to use BLOCK mode
      this.handleServerOutput ("LOCAL> " + command);
      command += CRLF;
      commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
      reply = this.readCommandReply();
  
  
      command = "REST abcdef"; //
      this.handleServerOutput ("LOCAL> " + command);
      command += CRLF;
      commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
      reply = this.readCommandReply();
  
      command = "RETR test.txt"; //
      this.handleServerOutput ("LOCAL> " + command);
      command += CRLF;
      commandSocket.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
      reply = this.readCommandReply();
  
      // Now read data from the dataSocket
      Byte[] readBytes;
      ArrayList byteArray = new ArrayList();
      int sizeReceived;
      string serverMessage = "";
  
      FileStream oFileStream = new FileStream("C:\\download.txt" , FileMode.Create);
  
      do {
        readBytes = new Byte[DATA_BLOCK_SIZE];
        sizeReceived  = dataSocket.Receive(readBytes, readBytes.Length, SocketFlags.None);
        serverMessage  += Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
        byteArray.Add(readBytes);
        oFileStream.Write(readBytes,0, sizeReceived);
        
      }
      while (sizeReceived == readBytes.Length ); // Go back for more if necessary
  
  
  //    System.Console.WriteLine (serverMessage.Length);
  //    Byte[] fileByteArray = Encoding.ASCII.GetBytes(serverMessage);
        // SAVE TO DISK:
  
  
  //      Array.Copy(byteArray.ToArray(), writeBytes, byteArray.Count);
        oFileStream.Close();
      
      reply = this.readCommandReply();
    }
*/

⌨️ 快捷键说明

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