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

  • Home
  • SEARCH
  • 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 7414411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:05:46+00:00 2026-05-29T07:05:46+00:00

I have two service. 1- client 2- server. Both the services communicate through socket.

  • 0

I have two service. 1- client 2- server. Both the services communicate through socket.
Client contains a file system watcher. Whenever the file system watcher detects new file in the specified folder it establishes the connection to sever and sends the file to server.
Server listens to a specific port and accepts the request and receives the file and saves the details to DB. And then sends the success/error message to client.

For the first time the whole system is working fine. But when the filesystemwatcher gets the 2nd file, the application generates the exception “The process can’t access the file. Its being used by another process”. Again when I debug the service there is no exception. Not able to get the exact reason of the issue.

Any help will be highly appreciated.

Client code:

namespace WindowsService1
{
public partial class Client : ServiceBase
{
    string hostIPAddress = string.Empty;
    string processedFilePath = string.Empty;
    int hostPort;
    Socket socketClient;
    IPEndPoint remoteEndPoint;
    FileStream fs;

    public PCMParserClient()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        hostIPAddress = ConfigurationManager.AppSettings["HostIP"];
        hostPort = int.Parse(ConfigurationManager.AppSettings["HostPort"]);
        //File system watcher
        FsWatcher.Path = ConfigurationManager.AppSettings["FileWatcherPath"];
        processedFilePath = ConfigurationManager.AppSettings["ProcessedFilePath"];
    }

    protected override void OnStop()
    {
    }

    private void FsWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        try
        {
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);               
            IPAddress remoteIPAddress = IPAddress.Parse(hostIPAddress);
            remoteEndPoint = new IPEndPoint(remoteIPAddress, hostPort);
            //Establish the connection to server
            socketClient.Connect(remoteEndPoint);
            EventLog.WriteEntry(e.Name+"before");

                **//the below line generates teh exception for 2nd time onwards**
                 using (fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                 {
                     EventLog.WriteEntry("1");
                     //Convert the file name in form of byte
                     byte[] fileNameByte = Encoding.ASCII.GetBytes(e.Name);
                     EventLog.WriteEntry(e.Name);

                     //4- to store the filename length(as int - 4bytes)
                     //8- to store the file content length(as long take 8 bytes)
                     int totalLength = 4 + fileNameByte.Length + 8;

                     //Clientdata[] reprents the data to sent to the server 
                     //which represent the file details
                     byte[] clientData = new byte[totalLength];
                     byte[] fileNameLength = BitConverter.GetBytes(fileNameByte.Length);
                     byte[] fileContentLength = BitConverter.GetBytes(fs.Length);

                     //Copy all the data ClientData array
                     fileNameLength.CopyTo(clientData, 0);
                     fileNameByte.CopyTo(clientData, 4);
                     fileContentLength.CopyTo(clientData, 4 + fileNameByte.Length);

                     //Send the data to server
                     socketClient.Send(clientData);

                     int byteRead = 0;
                     int bytesToRead = (int)fs.Length;

                     while (bytesToRead > 0)
                     {
                         byte[] data = new Byte[1500];
                         byteRead = bytesToRead > 1500 ? 1500 : bytesToRead;
                         int n = fs.Read(data, 0, byteRead);

                         //Send the data to server
                         socketClient.Send(data);
                         bytesToRead -= n;
                     }

                     fs.Flush();
                     //fs.Close();
                     //fs.Dispose();
                 }


                //Code block to get the success/failure message from server
                byte[] message = new byte[5];
                int msg = socketClient.Receive(message);

                else if (Encoding.ASCII.GetString(message).Contains("Error"))
                {
                    throw new Exception("Error occured while processing the file " + e.Name);
                }

        }
        catch (SocketException ex)
        {
            ExceptionLogger.LogException(ex);
        }
        catch (IOException ex)
        {
            ExceptionLogger.LogException(ex);
        }
        catch (Exception ex)
        {
            ExceptionLogger.LogException(ex);
        }
        finally
        {
            if (socketClient != null && socketClient.Connected)
            {
                socketClient.Close(); 
            }
        }
    }
}

}

Server Code:

namespace PCMParserService
{
public partial class ParserServer : ServiceBase
{
    Socket serverSocket = null;
    public Timer timer1;
    IPEndPoint ipEndPoint;

    public HL7ParserService()
    {
        InitializeComponent();
        timer1 = new Timer(1000);
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
    }

    void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
        Socket handler = null;
        try
        {
            // The program is suspended while waiting for an incoming connection.
            // This is a synchronous TCP application
            handler = serverSocket.Accept();

            byte[] fileDetails = new byte[1500];
            //Recieve the file details
            handler.Receive(fileDetails);
            int fileNameLength = BitConverter.ToInt32(fileDetails, 0);
            string fileName = Encoding.ASCII.GetString(fileDetails, 4, fileNameLength);
            EventLog.WriteEntry(fileNameLength.ToString(), System.Diagnostics.EventLogEntryType.Information);
            int fileLength = BitConverter.ToInt32(fileDetails, 4 + fileNameLength);

            filePath = ConfigurationManager.AppSettings["ProcessedDirectory"];

            FileStream fs = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write);
            int byteRead = 0;

            while (byteRead < fileLength)
            {
                byte[] data = new Byte[1500];
                //Recieve the data and write to the file
                int r = handler.Receive(data);
                fs.Write(data, 0, r);
                byteRead += r;
            }
            fs.Flush();
            fs.Close();
            fs.Dispose();


            //-Code to Parse text file and save to db
            FileStream fileStream = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fileStream);
            ///Code   
            fileStream.Close();
            fileStream.Dispose();
            sr.Close();
            sbMessage.Append("</Message>");
            saveFileDetails(sbMessage.ToString());

            //-- End of File Parsing code
            handler.Send(Encoding.ASCII.GetBytes("Done"));
        }
        catch (SocketException ex)
        {
            ExceptionLogger.LogException(ex, "Error occured while processing the file + " + filePath);
            handler.Send(Encoding.ASCII.GetBytes("Error"));
        }
        catch (IOException ex)
        {
            ExceptionLogger.LogException(ex, "Error occured while processing the file + " + filePath);
            handler.Send(Encoding.ASCII.GetBytes("Error"));
        }
        catch (SqlException ex)
        {
            ExceptionLogger.LogException(ex, "Error occured while processing the file + " + filePath);
            handler.Send(Encoding.ASCII.GetBytes("Error"));
        }
        catch (Exception ex)
        {
            ExceptionLogger.LogException(ex, "Error occured while processing the file + " + filePath);
            handler.Send(Encoding.ASCII.GetBytes("Error"));
        }
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            //The port on which the server listens
            ipEndPoint = new IPEndPoint(IPAddress.Any, 8030);

            //Defines the kind of socket we want :TCP
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //Bind the socket to the local end point(associate the socket to local end point)
            serverSocket.Bind(ipEndPoint);

            //listen for incoming connection attempt
            // Start listening, only allow 100 connection to queue at the same time
            serverSocket.Listen(100);

            timer1.Start();
        }
        catch (SocketException ex)
        {
            ExceptionLogger.LogException(ex, string.Empty);
        }
        catch (Exception ex)
        {
            ExceptionLogger.LogException(ex, string.Empty);
        }
    }

    protected override void OnStop()
    {
        timer1.Stop();
    }

    protected override void OnPause()
    {
        timer1.Stop();
    }

    protected override void OnContinue()
    {
        timer1.Start();
    }

    protected override void OnShutdown()
    {
        timer1.Stop();
    }

}
}
  • 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-29T07:05:47+00:00Added an answer on May 29, 2026 at 7:05 am

    This is usually caused by the process writing the file having not finished writing the file, and thus releasing the file, when the FileSystemWatcher notifies you that there’s a new file in the watched directory.

    Options to mitigate this include:

    • Having a delay after the notification before you process the file
    • Processing the file immediately but putting any files which result in “The process can’t access the file. Its being used by another process” into a queue to be retried periodically.

    You won’t usually see this issue during debugging/testing because you’ll be either manually copying files into the watched directory or stepping-through your code slowly enough that the file has finished being written prior to the attempt to open it.

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

Sidebar

Related Questions

I have two webapps: a web-service client and a server (both CXF-based, using the
I have two applications: c++ service and a RoR web server (they are both
I have two Services called TemplateService, TemplateReportService (both defined in one WCF Service Library)
I have two client-side RIA services projects, each mapped to server-side RIA assembly for
I have a WCF client/service app that relies on secure communication between two machines
Let's say I have two windows services running. One service is 'Polling' - it
I have two WCF RESTful services - the general service is public and has
I'm developing an axis java web service with two methods, both have an custom
I have two WCF clients consuming a 3rd party web service. These two clients
I have two instances running of same Windows Service. They check the health of

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.