constantly checking internet connection

  • Thread starter Thread starter Nikola Bucic
  • Start date Start date
N

Nikola Bucic

Could someone tell me how can I constantly check if I'm connected to
internet and if possible to determine what type of connection is it.

My best shot wright now is to have a timer which will periodicaly call
API to check whether am I connected or not
 
Hi,

And that's the best option IMO, in the handler you could check for the kind
of connectivity you want and send an event to the UI in case you need to.
 
If you want to check if there is an ability to connect to specific server on
the internet you can
check internet connection, performimg basic network operation ( connection )
System.Net.Sockets

TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("your_server.com", 80);
//Connection is present
tcpClient.Close();

Generally connection means that there will be server and client. As I
understood you're in the client's role?
if so it is only necessary to check the accessability of the server in order
to determine if connection is present.
 
it's not important whether I'm on server or on client side. I just want
to have constantly monitoring over internet connection and when my PC
become online run some code
 
You mean hardware connection? Smth like when ethernet cable is plugged or
unplugged?
 
You can develop an windows Service which will fire an API to check whether
ur PC is conencted to Internet or not
and You can get its status using Notify Icon.
 
Nikola Bucic said:
it's not important whether I'm on server or on client side. I just want
to have constantly monitoring over internet connection and when my PC
become online run some code

"Internet connection" is very hard to define. You can be connected over LAN,
through ethernet. You may have dial-up connection, wireless, DSL or
something else. Even if your ethernet interface is up, that does not mean
that you can connect to internet. There may be firewalls that restrict
access (totally or to some services). IMHO, if you need to connect to some
server to do some work, don't check for internet connection, check for
servers availability. Try to connect and if it works, it works :)

Regards,
Goran
 
Rahul, I want it to be a Service, but can you tell me do I need timer
to check Connection state or is there some windows message(s) that I
can trap?

Goran Sliskovic, I call API InternetGetConnectedState. Regarding
defining Internet connection, I'm quite satisfied with that API.
Here is dllCall for mentioned API

[DllImport("Wininet", CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState(ref
InternetConnectionState lpdwFlags, int dwReserved);
 
Nikola Bucic wrote:

[...snip...]
Goran Sliskovic, I call API InternetGetConnectedState. Regarding
defining Internet connection, I'm quite satisfied with that API.
Here is dllCall for mentioned API
[...snip...]

Actually, it's impossible to correctly identify if a computer is connected
to "the internet" or not.

InternetGetConnectedState just reads the settings of the default connection
of IE returning "true" whenever the connection is to be established via a
LAN. If you are using Dial-Up-Connections it does not take into account how
autodial is set up (at least not correctly). Finally, it needs a complete
installation and configuration of at least IE 4 to work at all.
 
Back
Top