Ankit
Ankit

Reputation: 133

Sending files from Client to Server

I am trying to send multiple files to a server ever 2 seconds and after the file is sent it is deleted and the program is restarted as to again generate the file which needs again to be sent to server.

But it works till the time the server is online...In case the sever is restarted all the program functions will work fine but when this function is invoked it keeps on displaying "No connection could be made because the target machine actively refused it" no matter even when the server comes online in between..

        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();
                    }
                }
            }
        }
    }

What i want is of the server goes offline or "No connection could be made because the target machine actively refused it" is displayed...the program should continue with the loop and move unhindered until server comes online and the updated file will be sent to the server.

Upvotes: 1

Views: 2588

Answers (1)

NightDweller
NightDweller

Reputation: 923

Well, you're exception handling is faulty. You are catching (Exception ex) and printing out the error message you quoted. It's entirely possible that the ACTUALL exception has to do with the files you are writing and erasing. perhaps some files failed to erase or be opened.

Looking at the code, i suspect the problem is that you're never actually closing the socket if there is an exception.

You should add the following to the finally clause.

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

And you should print out the actual exception message to the error log so you know what's really going on when an error occurs.

Upvotes: 1

Related Questions