2011-05-11 160 views
1

我做了一個Windows服務的動機是將多個文件發送到指定的服務器。多個文件發送到服務器

 private void sendfile() 
    { 
     timer.Stop(); 
     RegistryKey theLocalMachine = Registry.LocalMachine; 
     RegistryKey theSystem2 = theLocalMachine.OpenSubKey(@"SOFTWARE\\NetworkUsagemonitoring\\", true); 
     RegistryKey interfacekey4 = theSystem2.OpenSubKey("Usagerecorder", true); 
     string serverno = interfacekey4.GetValue("serverno").ToString(); 
     for (int i = 0; i < netarr1.Length; i++) 
     { 
      for (int j = 0; j < netarr2.Length; j++) 
      { 
       if (netarr1[i].Name == netarr2[j]) 
       { 
        IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(serverno), 5656); 
        Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 
        try 
        { 
         if (File.Exists(@"C:\" + netarr1[j].Name + "_record.xml")) 
         { 
          fileName = (@"C:\" + netarr1[j].Name + "_record.xml"); 
          fileName = fileName.Replace("\\", "/"); 
          while (fileName.IndexOf("/") > -1) 
          { 
           filePath += fileName.Substring(0, fileName.IndexOf("/") + 1); 
           fileName = fileName.Substring(fileName.IndexOf("/") + 1); 
          } 
          byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName); 
          if (fileNameByte.Length > 850 * 1024) 
          { 
           return; 
          } 
          byte[] fileData = File.ReadAllBytes(filePath + fileName); 
          byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length]; 
          byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length); 

          fileNameLen.CopyTo(clientData, 0); 
          fileNameByte.CopyTo(clientData, 4); 
          fileData.CopyTo(clientData, 4 + fileNameByte.Length); 
          clientSock.Connect(ipEnd); 
          clientSock.Send(clientData); 
          clientSock.Close(); 
          recorded[j] = 0; 
          File.Delete(@"C:\" + netarr1[j].Name + "_record.xml"); 
         } 
         else 
         { 
          Update1Network_Interface(); 
         } 
        } 
        catch (Exception ex) 
        { 
         if (ex.Message == "No connection could be made because the target machine actively refused it") 
         { 
          LogEvent("No connection could be made because the target machine actively refused it", EventLogEntryType.Information); 
          break; 
         } 
        } 
        finally 
        { 
         if (clientSock != null) 
         { 
          LogEvent("Client Socket Closed", EventLogEntryType.Information); 
          clientSock.Close(); 
          sendfile(); 
         } 
        } 
       } 
      } 
     } 

     restart(); 
    } 

但是,因爲該服務文件來啓動其execution..there似乎有三種文件,這需要發送到服務器,但它往往只發送一個經常忽略了另一個......至於中的代碼片段在兩個for循環下,我正在檢查文件的存在,如果它們存在,那麼它們需要傳輸。

如在服務器端的測試目的以下是代碼

 private void Form1_Load(object sender, EventArgs e) 
    { 
     FTServerCode.receivedPath = (@"C:\Receiving\"); 

     if (FTServerCode.receivedPath.Length > 0) 
      backgroundWorker1.RunWorkerAsync(); 

    } 
} 
class FTServerCode 
{ 
    IPEndPoint ipEnd; 
    Socket sock; 
    public FTServerCode() 
    { 
     ipEnd = new IPEndPoint(IPAddress.Any, 5656); 
     sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 
     sock.Bind(ipEnd); 
    } 
    public static string receivedPath; 
    public static string curMsg = "Stopped"; 
    public void StartServer() 
    { 
     try 
     { 
      curMsg = "Starting..."; 
      sock.Listen(100); 

      curMsg = "Running and waiting to receive file."; 
      Socket clientSock = sock.Accept(); 

      byte[] clientData = new byte[1024 * 5120]; 

      int receivedBytesLen = clientSock.Receive(clientData); 
      curMsg = "Receiving data..."; 

      int fileNameLen = BitConverter.ToInt32(clientData, 0); 
      string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen); 

      BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ; 
      bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen); 

      curMsg = "Saving file..."; 

      bWrite.Close(); 
      clientSock.Close(); 
      StartServer(); 

     } 
     catch (Exception ex) 
     { 
      curMsg = "File Receving error."; 
     } 
    } 
} 

我要的是檢查文件是否存在,並將它們發送到服務器,如果有錯誤,則該文件應被重新傳輸....

任何幫助將得到高度認可.....

回答

0
上乍一看

,你是在內部循環使用netarr1 [J],請將.Name ......應該說是netarr1 [i] .Name(或netarr2 [j] .Name,因爲你只有在它們是equ時才執行人)??

也許i和j是很難在文本區分開來,也許更具描述性的NAEM

而且System.IO.Path.Combine是好事,對文件路徑使用....

+0

最初我有netarr1 [我]然後我把它改爲j,但沒有運氣在任何情況下 – Ankit 2011-05-11 09:55:33

+0

實際上文件正在生成這些似乎在傳輸過程中的一些問題,因爲當最初第一個文件傳輸,然後在服務器端它收到然後關閉套接字,並再次開始監聽,並在這之間我猜這兩個文件往往會得到錯誤信息,因爲文件傳輸失敗,只有循環中的第一個文件往往會傳輸到服務器.. – Ankit 2011-05-11 09:58:17

+0

我實際上看不出爲什麼你有2個循環 - 目前,如果我理解正確,你似乎只是在「for j」循環中處理netarr2的內容,而是在netarr1.Length循環中執行。我根本看不到任何來自netarr1(即外部,i,循環)的變化輸入。你能簡要概述一下邏輯的邏輯嗎?我真的會檢查你使用的文件名是你期望的(添加跟蹤代碼)。在System.IO庫中有很多用於解析和構建文件路徑和名稱的好功能。我發現這些讓我在「本土」代碼中犯錯誤。 – kpollock 2011-05-11 10:09:45