Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4603222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:06:48+00:00 2026-05-22T00:06:48+00:00

I have made a windows service the motive behind is to send multiple files

  • 0

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…..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:06:49+00:00Added an answer on May 22, 2026 at 12:06 am

    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….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been made a windows service.in which i have been added project installer.in
I have the task: I have the Windows-made rar-archive (folders, files in it and
I have a website for my gadget files and I’ve made a windows 7
I have a symlink on my Windows server which was made like this: F:\>mkdir
I have a windows service written in c#. It has a timer inside, which
I have made a new windows service which works fine using barebone code (just
I have a windows service that needs to access the files (sometime modification also)
I have a Windows Service that communicates with a server and acts as a
I have made one windows phone based application. i want some designing ideas from
I have made a small application in Windows Azure. For research purposes they asked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.