SocketException error code 10093

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?
 
G

Guest

Hello, Vadym!

All the calls to the first device are made through an unmanaged dll. All I
get back when the connection is lost is an error code basically telling me
the connection was lost. I don't do anything to handle the connection to that
device but everything is done inside the dll that I don't have access to.

I suspect that somewhere inside the dll something is done to invalidate all
the sockets. I cannot hope to get any help from the guys who did the dll so
what I need to do is to somehow recover from this state by catching the
SocketException and try to restart the TcpListener.

What do you think of this approach?

Any tips on how to recover from this state?

Thanks

/ Henrik
 
V

Vadym Stetsyak

Hello, Henrik!

H> What do you think of this approach?
H> Any tips on how to recover from this state?

I think that you can remove the part with TcpListener.Pending(...).

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
G

Guest

Hello agian Vadym,

If I remove the TcpListener.Pending() call I start to get the same error at
other calls to the TcpListener. I need to either create a new TcpListener or
somehow repair the one I got.

It is the connection to the other device I'm trying to detect using the
TcpListener but somehow the losing of connection from the first device is
messing up the connection (or listening after a connection) to the second
device.

How can I make the connection to thw second device more reliable?

It seems as I somehow need to reset the sockets or something?

How do I do that?

Regards, Henrik
 
G

Guest

Hello, Vadym!

Thank you for you input.

I ran a test where I don't get any incoming connections
(TcpListener.Pending() always return false)
Then I don't Accept any sockets and never start any communication from this
thread.

The same problem still occur with the TcpListener when the communication to
the other device is lost.

I've also tried to stop this thread and restart a new one with a new
TcpListener but the problem then occurs when I try to create a new
TcpListener object.

Any other suggestions ?

Regards, Henrik
 
L

Luke Zhang [MSFT]

If you try another port number with new tcplistener, will this correct it?

Regards,

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Changing the port is not an option in this case since I need to listen on a
specific port to be able to connect to the device (port 502 using modbus
protocol over tcp/ip)

However I've tried to change the port to test if it helps but there is no
change. Still the same error.

Regards Henrik
 
L

Luke Zhang [MSFT]

That looks strange. I suggest you may contact the supplier of devices to
see if they have same problem reported. The problem may be related to the
underlying API they provided.

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hello Luke

The supplier of the device tells me I need to call WSAStartup as a part of
my network initialization to make it more robust. I understand this i a
winsock function but I'm not sure wehter or not it is called by the methods I
use when starting listening for incoming connection.

Could you please confirm if this call i made or not by the code I've posted
earlier

Thank you,

Henrik
 
L

Luke Zhang [MSFT]

The TcpListener class in System.Net.Sockets namespace provides a managed
implementation of the Windows Sockets (Winsock) interface, so that we don't
need to call the underlying WinSock APIs. In other words, WSAStartup will
be called in the backaround.

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top