2010-07-29 229 views
0

我想在不使用FTPWebRequest的情況下使用C#在FTP中並行執行多個下載/上傳。 我已經寫了我的自定義代碼,當我嘗試同時下載兩個文件時,第一個獲得正確下載,而第二個顯示大小爲0 KB(它也下載)。使用C#在FTP中並行使用多個下載/上傳

public void sendCommand(String command, params string[] strfilename) 
{ 
    if (command == "RETR ") //Downloading file from Server 
    { 


     FileStream output = null; 
     if (!File.Exists(strfilename[0])) 
     output = File.Create(strfilename[0]); 

     else 
     output = new FileStream(strfilename[0] , FileMode.Open); 

     command = "RETR " + strfilename[0]; 

     Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray()); 
     clientSocket.Send(cmdBytes, cmdBytes.Length, 0); 
     Socket csocket = createDataSocket(); 

     DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds); 

     while (timeout > DateTime.Now) 
     { 
      this.bytes = csocket.Receive(buffer, buffer.Length, 0); 
      output.Write(this.buffer, 0, this.bytes); 

      if (this.bytes <= 0) 
      { 
       break; 
      } 
     } 
     // this.BinaryMode = true; 
     output.Close(); 

     if (csocket.Connected) csocket.Close(); 

     this.readResponse(); 
     MessageBox.Show("File Downloaded successfully"); 

     else if....so on 

    } 
} 

在我的主要方法,我這樣做:

ftpcommand.sendCommand("RETR ","RMSViewer.xml"); //Downloading from Server 
ftpcommand.sendCommand("RETR ","cms.xml");//Downloading from Server 

任何代碼片段....

回答

0

戴維說,你需要你的ftpCommand類的單獨的實例。看看使用BackgroundWorker在後臺運行命令(異步)。

+0

@從服務器下載 ftpcommand.sendCommand帕特里克:那麼如果有100個文件我必須創建100個instaces? – 2010-08-05 12:07:11

+0

我懷疑你可以同時下載100個文件 - 大多數FTP服務器限制了單個IP可以進行的連接數量(以防止單個人超負荷服務器)。您的原始問題是關於同時下載兩個文件通過您的ftpCommand類和兩個BackgroundWorker類的兩個不同實例,它是可行的。 – PatrickSteele 2010-08-05 12:14:43

+0

@Patrick:2個實例?你能告訴我在上面的代碼中創建2個實例的位置。謝謝。 – 2010-08-05 12:44:47

0

你是如何同時發出您的要求?

螺紋?

如果是這樣 - 你可能想要確保你創建了你的「ftpcommand」類的單獨實例。

,我認爲我們需要更多的信息,能夠幫助您:)

+0

雅像我想知道如何我可以創建線程上它同時requests.Sepearte實例意味着我沒有得到你。 ftpcommand.sendCommand(「RETR」,「RMSViewer.xml」); //從服務器下載 ftpcommand.sendCommand(「RETR」,「cms.xml」); //從服務器下載 – 2010-07-29 12:01:22