Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / June 2004

Tip: Looking for answers? Try searching our database.

testing for network connectivity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 18 Jun 2004 16:09 GMT
Does anyone know of a simple and fast way to test for network connectivity
in C#?
I have a class that makes UDP send calls sixty times a second using the
UDPClient class.
I would prefer not to pay the performance penalty for catching an exception
sixty times a second
when network connectivity is down, if there is a better way to test for
connectivity. Any ideas?

Thank You,

-Jim
"Ying-Shen Yu[MSFT]" - 21 Jun 2004 04:32 GMT
Hi Jim,

From your description, I'd like to confirm if my understanding is correct,
you are trying to determine if a client is able to connect to certain
server, not just if a client has a network connection( which might not be
able to connect to the destination server) , is it correct?

IMO, there is no better way to resolve this issue, the tcp "connection" is
simulated by "time-out and retry" mechanism on the two end points, the
underlying network routers do not have the concept of connection state, in
other words, we could not know a tcp connection failure until we get
timeout when we try to send something to the destination.

Your current solution is ok. If you can reach the destination server using
ping command , you may try P/Invoking the IsDestinationReachable API, it
provides a similiar function by sending ping packets to the destination and
return a boolean value instead of exception, maybe it could reduce the
performance penalty somehow.  You may define the P/Invoke definition of
this API in C# like below:
//bool bReachable = IsDestinationReachable("www.microsoft.com",IntPtr.Zero);
[DllImport("sensapi.dll")]
private extern static bool IsDestinationReachable(string dest,IntPtr ptr);

Ofcourse, many servers turned off the Ping response for security concerns,
if this is the case, maybe we at least could polling the status in a
seperate thread.
Also, If both the server and clients are in the same LAN, maybe you can try
making  a "heartbeat" program on the server side, this program could create
a multicast group and send echo message to the group address periodically,
then the client could get the connection status by receiving echo messages.

If you still have problem on this issue, please feel free to replyl this
thread.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
Yan-Hong Huang[MSFT] - 23 Jun 2004 08:13 GMT
Hi Jim,

This is a FAQ in windows programing. Please refer to this article that I collected before. It is talking about connecting to Internet. But you can
also get some idea from it.

--------------
Actually, there is no single function for determining if a machine is connected to the Internet, and it is impossible to reliably determine what
is happening without side effects - such as automatic network connections taking place. What you can do is reliably detect when there
definitely isn't an Internet Link: in the absence of any dial up or LAN connection the system is definitely off line.

Some techniques include:

1. IsNetworkAlive()
If you are targeting system with IE5 or later, this is the best API call yet it even listens for traffic on a LAN. There is a secondary function
IsDestinationReachable() which tries to resolve the hostname and ping it. This does not work through firewalls, and overestimates speed as
the max the LAN card can support, rather than the actual point to point bandwidth.

2. RasEnumConnections()
A reliable technique for modems and direct dial up networking, but not for situations where Internet access is via a LAN. You should
dynamically load "RasEnumConnectionA" from "RASAPI32.DLL", as LAN installations of Windows may not include the library.

3. InternetGetConnectedState()
This Wininet /IE4 function call can distinguish between modem and LAN, but can't handle complex LAN+autodial router situations. It is "offline
state aware". Important: handling of the offline flage changed for IE5 -it returns TRUE for connected'  even when off line, but signals the flags in
the LPDWORD parameter.

4. InternetCheckConnection()
A Winnet/IE4 function call. This is meant to determine if a URL is reachable- in practice it is pretty unreliable and best voided.

5. NT SP4, NT5: The IP helper API can tell you which network interface to use to connect to a supplied IP address, and what the bandwidth
and current status of that link is

6. Using the Offline flag which is part of IE4 to allow users to manually control the online/offline state of applications. This flag is stored in
the registry and can be manipulated via some funcions calls

These calls mostly determine the presence or absence of network connections -not Internet access, so can't handle a home network sharing
a dial up connection, or two laptops connected directly to each other.

The global offline state flag of IE4 (and hence win98, NT5) and the call to test it - InternetGetConnectedState()- look the best long term options,
but will take time to become universal. The IP Helper APIs even let you find out how much traffic is going over a link, but only detect the
'loopback' interface on Windows 98, so is not a lot of use. Wouldn't a 'GetSpeedToHost() function call be great?

Finally, whatever technique you use, when it's time to talk to a remote site, always add timeouts or a cancel button. Even a quick functions like
gethostbyname() can lock up an app if something in the network chain is broken.
--------------

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ?C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.