I have made a windows service the motive behind is to send multiple files to a designated server.
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();
}
But as this service file start its execution..there seem to be three files which needs to sent to the server but it tends to send only one constantly neglecting the other one...As in the code snippet under the two for loops i am checking the existence of the files and if they are present then they need to be transmitted.
As in the server side for a testing purpose following was the code
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.";
}
}
}
What i want is to check the existence of files and transmit them to the server and if there is an error then the file should be re transmitted....
Any help will be highly acknowledged.....
on a first glance, you are using netarr1[j].Name in the inner loop... should that be netarr1[i].Name (or netarr2[j].Name since you are only executing when they are equal)??
Maybe i and j are hard to distinguish in text, maybe a more descriptive naem
Also System.IO.Path.Combine is good to use on file paths....