0

I'm running Win service in server 2012 R2, service connection always dropping and I'm getting below error, I also share the TCP connection code.

Error Message:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. InnerExceptionMessage An existing connection was forcibly closed by the remote hostStackTrace at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

try
            {
                IsStop = false;
                TcpClient client = null;
                TcpListener server = new TcpListener(IPAddress.Parse(ipAddress), port);
                server.Start();
                IsRunning = true;
                
                while (IsRunning)
                {
                    client = await server.AcceptTcpClientAsync();                   
                    try
                    {
                        using (NetworkStream stream = client.GetStream())

                        {                            
                            try
                            {
                            byte[] dataArray = new byte[36];
                                var bytesRead = stream.Read(dataArray, 0, 36);
                                if (dataArray[0] != 0)
                                {
                                    if (processString(dataArray, 4, 7) != 0)  // check ID 
                                    {
                                        Thread thread = new Thread(new ParameterizedThreadStart(DataProcessThreadAsync));
                                        object[] obj = new object[] { dataArray, thread };
                                        thread.Start(obj);
                                    }

                                }                              
                                stream.Close();    
                                stream.Dispose();
                                
                            }
                         catch (Exception ex)
                        {
              
                            LoggerHelper.Instance.LogError(ex);
                        }
                    }
                }
                    catch(Exception ex)
                    {
                        LoggerHelper.Instance.LogError(ex);
                        
                    }
                

                IsStop = true;
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Instance.LogError(ex);
             
            }

Please help me with this, Thanks in advance.

Dani
  • 1,825
  • 2
  • 15
  • 29
Sanju
  • 1
  • 2
  • see: https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f – Mikael Aug 06 '20 at 05:32
  • You are aware that TCP is a stream? And while reading, you might get only a few bytes, partial messages etc. You should read until you read the data you need. – Jeroen van Langen Aug 06 '20 at 05:41
  • Errors happen, things go wrong sometimes. But you should shutdown a stream, wait for the other end to shutdown too, then close and dispose. – Jeremy Lakeman Aug 06 '20 at 05:54
  • Why mix async with sync I/O? Why mix `Task` with explicit spinning up of `Thread`s? –  Aug 06 '20 at 06:21

0 Answers0