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.