Checking internet connection

  • Thread starter Thread starter basulasz
  • Start date Start date
B

basulasz

I want to check internet connectivity within a program. For example I want
the program to ping microsoft.com and to determine that there is a connection
according to ping responses.

How can I do that?
 
I want to check internet connectivity within a program. For example I
want
the program to ping microsoft.com and to determine that there is a
connection
according to ping responses.

How can I do that?

Did you look at the Ping class? (in System.Net.NetworkInformation)
 
No. Actually I haven't known about anything about that class. I am new in
..net, and haven't used this namespace before
 
If you want a general API, IsInternetConnected will do it:
http://msdn2.microsoft.com/en-us/library/aa366143(VS.85).aspx
but for a specific site you'd need another test, like ping.

What are you trying to do? For example a ping to a specific location will
tell you about a physical connection, but opening a browser window to it can
fail if IE has phishing or security blocking of that site.
 
I have a Windows service application which uses a web service to send SMS
messages to cell phones, and it requires an available internet connection, so
i need to ping a specific location, and I think ping class in .NET will be
helpful for this purpose.

Thanks a lot...

Phil Wilson said:
If you want a general API, IsInternetConnected will do it:
http://msdn2.microsoft.com/en-us/library/aa366143(VS.85).aspx
but for a specific site you'd need another test, like ping.

What are you trying to do? For example a ping to a specific location will
tell you about a physical connection, but opening a browser window to it can
fail if IE has phishing or security blocking of that site.
--
Phil Wilson
[MVP Windows Installer]

basulasz said:
I want to check internet connectivity within a program. For example I want
the program to ping microsoft.com and to determine that there is a
connection
according to ping responses.

How can I do that?
 
I hacked up this little helper class that will allow you to check if you're
connected to the internet without having to ping a specific site. The
method you need to call is NetUtils.IsConnectedToInternet().

/// <summary>
/// A collection of network related methods.
/// </summary>
public class NetUtils
{

/////////////////////
// Imported functions
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState
(out int nDescription,
int nReserved);

/////////////
// Operations
/// <summary>
/// Checks if a connection to the internet exists.
/// </summary>
/// <returns>true if a connection to the internet exists.</returns>

public static bool IsConnectedToInternet()
{
int nDesc = 0;
if (!InternetGetConnectedState (out nDesc, 0))
return (false);
return (true);
}
}

Cheers,

/ravi
 
Back
Top