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(IPA ddress.Any, TcpipPort);
TcpListener.Sta rt();
try
{
do
{
// Avoid blocking calls
if (!TcpListener.P ending())
{
// Wait 500 ms before checking again
Thread.Sleep(50 0);
continue;
}
// Accept incoming connection
Socket socket = TcpListener.Acc eptSocket();
// Do things with the new socket
}
while (true); // Loop until thread is aborted
}
finally
{
TcpListener.Sto p();
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.Sock ets.SocketExcep tion from the
TcpListener.Pen ding() above whit the error code 10093.
Does anyone have any ideas on what might have happened? 9 6811
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(IPA ddress.Any, TcpipPort); H> TcpListener.Sta rt();
H> try H> { H> do H> { H> // Avoid blocking calls H> if (!TcpListener.P ending()) H> { H> // Wait 500 ms before checking again H> Thread.Sleep(50 0); H> continue; H> }
H> // Accept incoming connection H> Socket socket = TcpListener.Acc eptSocket();
H> // Do things with the new socket
H> } H> while (true); // Loop until thread is aborted H> } H> finally H> { H> TcpListener.Sto p(); 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.Sock ets.SocketExcep tion from the TcpListener.Pen ding() 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
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.Pen ding(...).
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Hello agian Vadym,
If I remove the TcpListener.Pen ding() 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.Pen ding(...).
-- Regards, Vadym Stetsyak www: http://vadmyst.blogspot
Hello, Vadym!
Thank you for you input.
I ran a test where I don't get any incoming connections
(TcpListener.Pe nding() 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.Pen ding() 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.Acc eptSocket(...) 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.Acc eptSocket();
// start thread that will handle network I/O System.Threadin g.ThreadPool.Qu eueUserWorkItem (new WaitCallback(Th readProc), socket); } while (someCondition) ; // Loop until condition is specified
static void ThreadProc(Obje ct stateInfo) { Socket socket = stateInfo as Socket; if ( socket == null ) return; //Do network I/O here }
-- Regards, Vadym Stetsyak www: http://vadmyst.blogspot
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.)
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.)
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.)
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.)
The TcpListener class in System.Net.Sock ets 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.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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" when connecting
from the client side.
However i am getting the following Exception:
|
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 socket:
+ EnableBroadcast 'this.client.EnableBroadcast' threw an exception of
type 'System.Net.Sockets.SocketException' bool
|
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 code============
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
|
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 Installing a network packet reading program called WireShark my
VB.Net program no longer runs and generates the following error :
An unhandled exception of type 'System.Net.Sockets.SocketException'
|
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 machine actively refused it
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:...
| |
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. The handler works
most of the time, but occasionally the page load hangs on the loading of the
last .res reference for a javascript file (Fiddler shows me this), and when
I stop the load in the browser, I get this error message:
A first chance...
|
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 from database entries, and returns the file to the user.
This has worked well in general. However, a particular file is giving me problems. It compiles a total of 1800 entries from the database, and before it returns the CSV file, it gives...
|
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 information and then disconnects.
Problem is the client, is the harder to manage, I use blocking mode to receive, after the timeout receive is supposed to throw a socketexception (according to the docs) but it never does... please help me... the code...
|
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 it
tries to establish a socket to the mail server it fails.
Just wondering someone has encountered this before, and if someone
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |