tags:

views:

87

answers:

4

is there any way to check internet connection status in linux using mono

A: 

You could try to open your connection as it is needed. If that fails display an error message. Alternatively, if you really need a general check (e.g. at application start) you could try to make HTTP requests to one or more omnipresent websites like google.com. (Or what ever protocol you mean by "internet").

Check out HttpWebRequest.

bja
I need something similar to InternetGetConnectedState API in windows
JKS
HttpWebRequest sometimes blocked by firewall
JKS
+1  A: 

If it's desktop app, you could query NetworkManager (which is the network connection manager on most Linux desktops) over d-bus, using the NDesk.DBus library.

See Banshee for an example: http://git.gnome.org/cgit/banshee/tree/src/Core/Banshee.Services/Banshee.Networking/NetworkManager.cs

mhutch
In fact, most of the files in that directory are probably of interest to you: http://git.gnome.org/cgit/banshee/tree/src/Core/Banshee.Services/Banshee.Networking/ for example, Wicd is another reasonably common network manager that can also be accessed over dbus.
mhutch
+1  A: 

Apart from what Michael already suggested for a desktop application, you can also do something like:

foreach (NetworkInterface ni in NetworkInformation.GetAllNetworkInterfaces ()) {
       // Check that any or all of:
       //        -ni.OperationalStatus == OperationalStatus.Up
       //        -that ni.NetworkInterfaceType is ethernet or wireless80211
       //        -ni.GetIPProperties() has a gateway and a DNS server
       // ...
}
Gonzalo
A: 

No matter what you end up using, it won't be reliable.

I see it all the time with Windows Vista and 7 at home. I use a home network, so my computers are always "connected." However, they are not always connected to the Internet.

That said, I would recommend checking the network interfaces as Gonzalo said. It is your best bet.

I would not rely on NetworkManager being present. I hate that thing and turn it off whenever I can. It is huge, ungainly, has an ugly name, relies on junk like HAL and DBUS. Early versions permanently put me off because they didn't work unless you were logged in to a GUI. It also collected bug work-arounds for wifi that were completely ridiculous in an open-source operating system that should have just fixed the original bugs. That led to other wifi managers and the command-line not being able to work properly and people being told to use NetworkManager, only because no one ever bothered to fix the actual bug!

Zan Lynx