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 8837063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:37:59+00:00 2026-06-14T09:37:59+00:00

I have an issue where, depending on the startup order of two network devices,

  • 0

I have an issue where, depending on the startup order of two network devices, I seem to lose the ability to receive data on one end.

The setup is one small server written in C# (.NET 4.0) that listens to a specific port for data coming from an industrial scanner, communicating over TCP. The scanner connects to the server application just fine, and will send small chunks of data (serial numbers) which the server receives and processes. However, restarting (actually, cycling power, doesn’t gracefully close its connection) on the scanner causes the server to stop processing data. The order of events, as well as the crucial network traffic that causes problems goes as follows:

             SUCCESS                           FAILURE
     |----------------------|             |----------------------|
     |  SCANNER  |  SERVER  |             |  SCANNER  |  SERVER  |
     |----------------------|             |----------------------|
     |           |   Starts |             |  Starts   |          |
     |   Starts  |          |             |           |  Starts  |
     |    Scan   | Received |             |   Scan    | Received |
     |  Restarts |          |             |  Restarts |          |
     --------(Network)-------             --------(Network)-------
     |    SYN    |          |             |    SYN    |          |
     |           | ACK-123  |             |           | SYN ACK  |
     | RST - 123 |          |             |    ACK    |          |
     |    SYN    |          |             |           |          |
     |           |  SYN ACK |             |           |          |
     |    ACK    |          |             |           |          |
     |----------------------|             |----------------------|  
     |    Scan   | Received |             |    Scan   |  !LOST!  |
     |----------------------|             |----------------------|      

As you can see, in the SUCCESS scenario, after the scanner restart, the server responds to the SYN with an ACK, and the scanner resets the connection because it realizes it is not in the right stream. The server detects this and gracefully handles it. However, in the FAILURE scenario, the server blissfully responds with a SYN ACK, scanner ACKs, and a new connection is established.

The code doesn’t complain about anything, but then my TcpLister/TcpClient never seems to receive any more data, even when it is received and ACKed as seen in Wireshark. Seemingly the TcpListener and handler are still waiting on the old connection for data? I’m new to this level network programming (and relatively new to C# itself), so definitely point out if I’m missing something obvious.

Code snippet of the server:

public void Run() 
 {
   while (!shutdown)
   {
     shutdown = false; // Reset
     listenerServer = new TcpListener(IPAddress.Any, scanner.Address.Port);

     try
     {
       listenerServer.Start();
       TcpClient handler = listenerServer.AcceptTcpClient();

       while (true)
       {
         try
         {
           incomingData = null;
           dataBuffer = new Byte[256];
           NetworkStream stream = handler.GetStream(); // blocks here
           int i;

           while ((i = stream.Read(dataBuffer, 0, dataBuffer.Length)) != 0)
           {
             incomingData += Encoding.ASCII.GetString(dataBuffer, 0, i);
             int startIndex = incomingData.IndexOf(startMarker);
             int endIndex = incomingData.IndexOf(endMarker);
             if (startIndex > -1 && endIndex > -1)
             {
               string serialData = incomingData.Substring(startIndex + 1, endIndex - startIndex - 1);
               scanner.ProcessDataChange(serialData);
             }
           }
         }
         catch (IOException e)
         {
           // Deals with SocketExceptions here...
         }
         finally
         {
           handler.Close();
         }
       }
     }
     catch (SocketException e)
     {
       listenerServer.Stop();
       listenerServer = null;
     }
     finally
     {
       listenerServer.Stop();
       listenerServer = null;
     }
   }
 }
  • 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-06-14T09:38:00+00:00Added an answer on June 14, 2026 at 9:38 am

    User @LB (L. B) had answered a question, but deleted it thinking it may have been irrelevant. In fact, it was a great answer to the question, and so I am reposting it here. In the end, I wasn’t listening to the new connections that were being made. Should L. B wish to repost his answer I’ll gladly accept it.

    Repost of L. B’s answer

    You accept only one connection with TcpClient handler = listenerServer.AcceptTcpClient();

    When the first connection is closed you don’t accept connections anymore. You should move it into the while loop.

    EDIT

    This is not a direct answer to you question. But I tried the below code and it works well. If you want, you can try it. I’ll remove this answer later.

     void Run()
     {
         TcpListener listener = new TcpListener(IPAddress.Any, 12345);
         listener.Start();
    
        Task.Factory.StartNew(() =>
        {
            while (true)
            {
                var client = listener.AcceptTcpClient();
                Task.Factory.StartNew(() => Handler(client), TaskCreationOptions.LongRunning);
            }
        }, TaskCreationOptions.LongRunning);
     }
    
     void Handler(TcpClient client)
     {
         using (NetworkStream stream = client.GetStream())
         {
             var dataBuffer = new Byte[256];
             int i;
             while ((i = stream.Read(dataBuffer, 0, dataBuffer.Length)) != 0)
             {
                 // Processes data here..
                 //var s = Encoding.UTF8.GetString(dataBuffer, 0, i);
                 //Console.Write(s);
    
             }
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue. I am getting data from a MySQL database, and make
I have the following issue with one of my Java programs. I'm trying to
I have an encoding issue - I have data stored in a MySQL table.
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
We are new to ROR, We have issue in creating Login/Logout process in ROR
Share your ideas please! I have issue to check the folder and convert a
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue with jquery and history.back(): I got a link: <a href=#
I have an issue of alphabetically sorting the contacts picked from the address book

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.