473,397 Members | 2,068 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,397 software developers and data experts.

Detect if Network and Internet Connection, the best way !!!


Hi all mister,

Which is THE BEST WAY IN THE WORLD AROUND for:

1. detect Network

2. detect Internet Connection is working...
which is the best way in the world and good practice recommended by MVPs
Microsoft ?

Thanks in advanced..

code below...but which is the best code ???

NetworkInterface.GetIsNetworkAvailable()

private static int ERROR_SUCCESS = 0;
public static bool IsInternetConnected() {
long dwConnectionFlags = 0;
if (!InternetGetConnectedState(dwConnectionFlags, 0))
return false;

if(InternetAttemptConnect(0) != ERROR_SUCCESS)
return false;

return true;
}
[DllImport("wininet.dll", SetLastError=true)]
public static extern int InternetAttemptConnect(uint res);
[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(long flags,long
reserved);

Correct translation below.

[DllImport("wininet.dll", SetLastError=true)] public static extern bool
InternetGetConnectedState(out int flags,int reserved);
I found code in C#,

HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
resp = (HttpWebResponse)req.GetResponse();

if(resp.StatusCode.ToString().Equals("OK"))
{
Console.WriteLine("its connected.");
}
else
{
Console.WriteLine("its not connected.");
}
}
catch(Exception exc)
{
Console.WriteLine("its not connected.");
}

--------------------------------------------------------------------------------
http://www.alhambra-eidos.es/web2005/index.html
--
http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.com/churrosoft
http://www.setbb.com/putainformatica...opic.php?p=843
www.trabajobasura.com/solusoft

Oct 29 '08 #1
1 6646
On Wed, 29 Oct 2008 05:27:01 -0700, Alhambra Eidos Desarrollo
<Al*********************@discussions.microsoft.com wrote:
Hi all mister,
Which is THE BEST WAY IN THE WORLD AROUND for:

1. detect Network

2. detect Internet Connection is working...
Try to communicate with the remote endpoint you're interested in.

Just because the computer locally says there's a network, that doesn't
mean there is one. Also, some computers are configured to only attach the
network when you try to use it.

The general rule for networking code when it comes to testing whether
something will work is: just try it, and check for errors. Because of the
way networks behave, it's almost never reliable to test in advance to see
whether a given operation will succeed. Also, almost all operations have
the chance of failure. So the only reliable thing to do is always handle
errors correctly, and don't bother checking in advance whether something
works. Just do it and handle the error if it happens.

Pete
Oct 29 '08 #2

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

Similar topics

1
by: ripken95 | last post by:
I connect to the internet through ADSL. I want to write the web page which can detect the connection status with javascript. This detection is like the signal detection in the mobile phone. If the...
6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
32
by: Victor | last post by:
I've been to a few websites where it displays the town, state (if USA), country I'm browsing from. Now, I know that detecting country is easy, but how do you determine the town? What ASP...
23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
0
by: Giovanni | last post by:
Hi, I am using VS 2005 - BETA 2. I'd like to use My.Computer.Network.IsAvailable to detect whether an internet connection is available. Is this the best "new" way of doing things or does the...
3
by: Dan Sikorsky | last post by:
I can send email using C# and SMTP, but keep closing the connection before the .eml file has left the Queue folder. The length of time the .eml file stays in the Queue folder is dependent on the...
8
by: BJ | last post by:
Problem: How can I code up a client side process to detect if the network is available? Synopsis: I am writing ASP.NET input forms for a Panasonic Tuff book. The users will be walking around...
5
by: Ke Tao | last post by:
HI All, Is there anybody have an idea of how to detect internet is reachable ? At present , I'm using ping to detect internet is reachable , but it's maybe a bad idea , some firewall of router...
3
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all mister, Which is THE BEST WAY IN THE WORLD AROUND for: 1. detect Network
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
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
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
Oralloy
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,...
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...
0
tracyyun
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...
0
isladogs
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...

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.