-2

I need to know how to get notified when a machine establishes internet connection successfully. i tried searching the code here and i found out some links like this

This is the link i found in stack overflow

NetworkChange.NetworkAvailabilityChanged.

can i use this?

Please help

Community
  • 1
  • 1
Akbar Ali
  • 295
  • 1
  • 6
  • 13
  • I am trying it. I need to make sure it will work and that it is the best way – Akbar Ali Sep 17 '12 at 06:56
  • Could you please provide more information; NetworkChange.NetworkAvailabilityChanged will only occur when the network availability of the network changes. This still doesn't necessarily indicate a valid internet connection. Maybe triggering on this to check the status of the network connectivity, then attempting to reach a known web server on the internet via ping would verify that you are connected to the internet. – Dutts Sep 17 '12 at 07:00
  • Hi @Richard: I have wrote a method like this NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); IPv4InterfaceStatistics statistics = face.GetIPv4Statistics(); if ((statistics.BytesReceived > 0) && (statistics.BytesSent > 0)) { return true; } – Akbar Ali Sep 17 '12 at 07:49
  • That still will only tell you if the NIC is receiving and sending packets, not that it is connected to the internet. If the machine was on a LAN with no internet connection this would still return true. – Dutts Sep 17 '12 at 08:01
  • @Richard : Thanks again There is an icon in the taskbar showing the internet connectivity. can we fire an event on change of that? if the function returns true, then we can try pining a site, right? – Akbar Ali Sep 17 '12 at 08:35
  • I am not entirely sure what Windows uses to distinguish between network and internet connectivity but I imagine it tries to reach an known internet address (maybe www.microsoft.com)? Using your code to check for network connectivity, and then attempting to make a connection to a known address on the internet would work. – Dutts Sep 17 '12 at 09:50
  • Exactly. But still, i have to call this method right on the machine connected to internet. How can i check that? For that can i use `NetworkChange.NetworkAvailabilityChanged` ? or i should set a timer and call the method on intervals? – Akbar Ali Sep 17 '12 at 10:05
  • Do both, because you have two states; whether the network is available (as denoted by the NetworkAvailabilityChanged event being fired) and then whether you can connect to the internet (which you can test by attempting to reach an internet site at intervals) – Dutts Sep 18 '12 at 10:27

1 Answers1

0

Have a look here: http://msdn.microsoft.com/en-us/library/ee264321%28VS.85%29.aspx

Does it cover what you want to do?

Daniel
  • 891
  • 6
  • 18