473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SocketException error code 10093

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?

Apr 25 '06 #1
9 6785
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

"Vadym Stetsyak" wrote:
Hello, Henrik!

H> The problem occurs when I start the other thread where the following
H> code is executed to wait for the other device to connect:

H> // Start listening
H> TcpListener = new TcpListener(IPAddress.Any, TcpipPort);
H> TcpListener.Start();

H> try
H> {
H> do
H> {
H> // Avoid blocking calls
H> if (!TcpListener.Pending())
H> {
H> // Wait 500 ms before checking again
H> Thread.Sleep(500);
H> continue;
H> }

H> // Accept incoming connection
H> Socket socket = TcpListener.AcceptSocket();

H> // Do things with the new socket

H> }
H> while (true); // Loop until thread is aborted
H> }
H> finally
H> {
H> TcpListener.Stop();
H> TcpListener = null;
H> ListenerThread = null;
H> }

H> When I lose connection to the first device while attempting to read
H> something from it the API doesn't signal that I've lost connection right
H> away but after about 10 seconds. Once I've got the signal that the
H> connection was lost I immediately after get a
H> System.Net.Sockets.SocketException from the TcpListener.Pending() above
H> whit the error code 10093.

How do you close connection?
Do you call Shutdown(... ) method on the device side?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot

Apr 26 '06 #2
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
Apr 26 '06 #3
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

"Vadym Stetsyak" wrote:
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

Apr 26 '06 #4
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

"Vadym Stetsyak" wrote:
Hello, Henrik!

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

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

That's strange, listening must not be affected by network errors.
TcpListener.AcceptSocket(...) returns socket that represents connection with remote host. If there are any errors on the connection then the socket that was returned from AcceptSocket() method suffers.

I think that the problem is in way you do processing with new socket.

IMO you can modify source code in the following way:
If number of devices will increase, then server can be redesigned
( http://msdn.microsoft.com/library/de...ogthrepool.asp )
( http://msdn.microsoft.com/msdnmag/is...s/default.aspx )

try
{
do {
// Accept incoming connection
Socket socket = TcpListener.AcceptSocket();

// start thread that will handle network I/O
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), socket);
}
while (someCondition); // Loop until condition is specified

static void ThreadProc(Object stateInfo)
{
Socket socket = stateInfo as Socket;
if ( socket == null )
return;
//Do network I/O here
}

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

Apr 26 '06 #5
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.)

Apr 28 '06 #6
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

"Luke Zhang [MSFT]" wrote:
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.)

Apr 28 '06 #7
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.)

May 1 '06 #8
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

"Luke Zhang [MSFT]" wrote:
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.)

May 1 '06 #9
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.)

May 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amit Yadav | last post by:
Hi, I am trying to write a simple TCP Client Server program which exchange some text messages. Both of my Client and Server are running on the same machine and hence i am using "localhost"...
0
by: oferns | last post by:
Hi, apologies for the cluncky title.... Using WinXpSP2 & VS2005 Express editions..... I am writing an asynchronous socket client and I am getting a SocketException error on creating the...
7
by: Lee | last post by:
Hey all, I'm using the following code to send stuff accross the network, appologies for it being in full, but I've really no idea exactly where this error is occuring. =======network...
2
by: Steve Lowe | last post by:
Hi, I have a VB.Net 2003 program that uses Indy to check if there are any messages waiting on a POP mail server. The program has been running fine for a few months, but last week after...
4
by: sd1978 | last post by:
Hi, I have placed a webservice in the webserver. When I access it from a webpage, default.aspx on a click of a button i get the following error: No connection could be made because the target...
5
by: PJ6 | last post by:
I can't figure this out. I have several references to style sheets and javascript files in my page header, the contents of which are served dynamically by my httphandler that serves *.res requests....
1
by: wesomon99 | last post by:
Hello, I have a JBoss web application (v4.04GA), run on an Apache Tomcat HTTP server (v5.5.17). It connects with a MySQL database (v4.1.10). My web application takes an XML file, creates a CSV file...
2
by: XenReborn | last post by:
Hi, I wrote a simple socket application, which connects to a simple socket server application I also wrote, the server opens a socket, listens, accepts connections, sends back a string of...
6
by: Python Programming on Win32 | last post by:
Hi, I have encountered a problem which I can not figure out a solution to. Tried Googeling it, but to no help unfortunately. The problem is running smtplib in a py2exe compiled exe file. When...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.