G
Guest
I'm developing an application that uses two threads to communicate with two
different devices on my network. One of the devices is reached through an API
from the supplier of that device. The API has support for detecting when the
connection to the device is lost and signal back to my application. When I
use this whitout starting the other thread everything works fine.
The problem occurs when I start the other thread where the following code is
executed to wait for the other device to connect:
// Start listening
TcpListener = new TcpListener(IPAddress.Any, TcpipPort);
TcpListener.Start();
try
{
do
{
// Avoid blocking calls
if (!TcpListener.Pending())
{
// Wait 500 ms before checking again
Thread.Sleep(500);
continue;
}
// Accept incoming connection
Socket socket = TcpListener.AcceptSocket();
// Do things with the new socket
}
while (true); // Loop until thread is aborted
}
finally
{
TcpListener.Stop();
TcpListener = null;
ListenerThread = null;
}
When I lose connection to the first device while attempting to read
something from it the API doesn't signal that I've lost connection right away
but after about 10 seconds. Once I've got the signal that the connection was
lost I immediately after get a System.Net.Sockets.SocketException from the
TcpListener.Pending() above whit the error code 10093.
Does anyone have any ideas on what might have happened?
different devices on my network. One of the devices is reached through an API
from the supplier of that device. The API has support for detecting when the
connection to the device is lost and signal back to my application. When I
use this whitout starting the other thread everything works fine.
The problem occurs when I start the other thread where the following code is
executed to wait for the other device to connect:
// Start listening
TcpListener = new TcpListener(IPAddress.Any, TcpipPort);
TcpListener.Start();
try
{
do
{
// Avoid blocking calls
if (!TcpListener.Pending())
{
// Wait 500 ms before checking again
Thread.Sleep(500);
continue;
}
// Accept incoming connection
Socket socket = TcpListener.AcceptSocket();
// Do things with the new socket
}
while (true); // Loop until thread is aborted
}
finally
{
TcpListener.Stop();
TcpListener = null;
ListenerThread = null;
}
When I lose connection to the first device while attempting to read
something from it the API doesn't signal that I've lost connection right away
but after about 10 seconds. Once I've got the signal that the connection was
lost I immediately after get a System.Net.Sockets.SocketException from the
TcpListener.Pending() above whit the error code 10093.
Does anyone have any ideas on what might have happened?