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();
}
}
}
}
}
我要的是服務器的聯機脫機或
‘無連接可以作出,因爲目標機器積極地拒絕它’將顯示 ...程序應該繼續循環並且不受阻礙地移動,直到服務器聯機並且更新的文件將被髮送到服務器。
謝謝朋友...它的工作.... – Ankit 2011-04-17 11:21:42
:)很高興有幫助 – NightDweller 2011-04-17 11:57:39