2011-04-17 122 views
1

我想發送多個文件到服務器有史以來2秒,文件發送後它被刪除,程序重新啓動,以再次生成需要再次發送到服務器的文件。發送文件從客戶端到服務器

但它一直運行到服務器在線時...如果服務器重新啓動,所有的程序功能都能正常工作,但是當這個功能被調用時,它會一直顯示「」因爲目標無法建立連接計算機主動拒絕」無論即使在服務器..

之間

 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]) 
       { 
        if (recorded[j] == 1) 
        { 
         try 
         { 
          IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(serverno), 5656); 
          Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 

          if (File.Exists(@"C:\" + netarr1[i].Name + "_record.xml")) 
          { 
           fileName = (@"C:\" + netarr1[i].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[i].Name + "_record.xml"); 
          } 
          else 
          { 
           UpdateNetwork_Interface(); 
          } 
         } 
         catch (Exception ex) 
         { 
          LogEvent("No connection could be made because the target machine actively refused it", EventLogEntryType.Information); 
          break; 
         } 
         finally 
         { 
          j++; 
         } 

        } 
        else 
        { 
         UpdateNetwork_Interface(); 
        } 
       } 
      } 
     } 
    } 
我要的是服務器的聯機脫機或 ‘無連接可以作出,因爲目標機器積極地拒絕它’將顯示 ...程序應該繼續循環並且不受阻礙地移動,直到服務器聯機並且更新的文件將被髮送到服務器。

回答

1

那麼,你是異常處理是錯誤的。您正在捕捉(Exception ex)並打印出您引用的錯誤消息。 ACTUALL例外完全有可能與您正在編寫和刪除的文件有關。也許有些文件無法刪除或打開。

看着代碼,我懷疑問題是,如果有異常,你永遠不會真的關閉套接字。

您應該將以下內容添加到finally子句中。

if (clientSocket!=null) 
    clientSocket.Close(); 

而且您應該將錯誤日誌中的實際異常消息打印出來,以便在發生錯誤時知道實際發生了什麼。

+0

謝謝朋友...它的工作.... – Ankit 2011-04-17 11:21:42

+0

:)很高興有幫助 – NightDweller 2011-04-17 11:57:39

相關問題