473,765 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

NetworkInterfac e.GetIsNetworkA vailable()

private static int ERROR_SUCCESS = 0;
public static bool IsInternetConne cted() {
long dwConnectionFla gs = 0;
if (!InternetGetCo nnectedState(dw ConnectionFlags , 0))
return false;

if(InternetAtte mptConnect(0) != ERROR_SUCCESS)
return false;

return true;
}
[DllImport("wini net.dll", SetLastError=tr ue)]
public static extern int InternetAttempt Connect(uint res);
[DllImport("wini net.dll", SetLastError=tr ue)]
public static extern bool InternetGetConn ectedState(long flags,long
reserved);

Correct translation below.

[DllImport("wini net.dll", SetLastError=tr ue)] public static extern bool
InternetGetConn ectedState(out int flags,int reserved);
I found code in C#,

HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest )WebRequest.Cre ate("http://www.google.com" );
resp = (HttpWebRespons e)req.GetRespon se();

if(resp.StatusC ode.ToString(). Equals("OK"))
{
Console.WriteLi ne("its connected.");
}
else
{
Console.WriteLi ne("its not connected.");
}
}
catch(Exception exc)
{
Console.WriteLi ne("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 6689
On Wed, 29 Oct 2008 05:27:01 -0700, Alhambra Eidos Desarrollo
<Al************ *********@discu ssions.microsof t.comwrote:
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
3358
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 signal is weak, the status may have one squares or zero on the phone. How could I do if I want to detect the status of the network? Thx and Best Regards.
6
4017
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
4282
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 components do this? Thanks, Bill.
23
6530
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 know if Window.Open has been redefined? BACKGROUND:
0
1527
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 above method above detect network connectivity of some other type? There seems to be a delay capturing the disconnect-connect events when I do use it, is there a way to shorten this delay? In essence, what does this method wrap? Regards,
3
3022
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 size of that file; big or many attachments increases the time. If I start an internet connection, issue the smtp mail send and then close the connection in my C# program, I need to wait until the .eml file has left the Queue folder. Otherwise,...
8
3031
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 the plant with a wireless connection. There are some pockets of non-connectivity. I've been tasked with disabling the submit button on the form if the network is unavailable. Possible solution: I can instantiate a timed process (VB.NET 2.0
5
2705
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 may block ping requesting. Best Regards, Ke Tao
3
3594
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
9399
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,...
0
10163
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, 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...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
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...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.