Link to home
Start Free TrialLog in
Avatar of Manuel Lopez-Michelone
Manuel Lopez-MicheloneFlag for Mexico

asked on

How to know if I am connected to the internet?

hi guys,

I have a cablemodem and I want to know if I am connected to the internet.

Any code available for that function?

best regards,
Manuel Lopez (lopem)
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

uses
  WinInet;


function IsConnected: Boolean;
begin
  Result := (InternetGetConnectedState(nil, 0))
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsConnected then
    ShowMessage('You are connected to the Internet')
  else
    ShowMessage('You are not connected to the Internet')
end;
Avatar of Manuel Lopez-Michelone

ASKER

Msasman,

Sorry, didnt work... I am on a local network and I tried your code in my machine. Didnt work. All the time I am connected even when Im not. I guess it was because I am part of a network. I took your code to the Server. Still the same problem. It didnt work.

Any ideas?

best regards
Manuel Lopez (lopem)
ASKER CERTIFIED SOLUTION
Avatar of Igor UL7AAjr
Igor UL7AAjr
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Igor

  but if you have firewall in the Lan, you couldn't ping the IPs out side your network, that's what we have :(
hi mnasman,

Seems something wrong with your firewall settings. I suppose that ports necessary to ping are closed. We have firewal too, and have no problems with pinging, of couse, rest of ports are closed excepting HTTP and FTP.

btw, there is another way to "ping" using HTTP. Just try to get some page from http server with low response time.

------
Igor.
yes Igor, I meant that
some adiministation closed the ports, so I think the second suggestion will be better, right
Hi lopem,

try this code

The following code is a possibility to detect a connection to the internet,
altough it is not 100%.

I have tested it on Windows NT with LAN connections (DSL and cable)
and it worked in these cases.


   
 const
  INTERNET_CONNECTION_MODEM           = 1;
  INTERNET_CONNECTION_LAN             = 2;
  INTERNET_CONNECTION_PROXY           = 4;
  INTERNET_CONNECTION_MODEM_BUSY      = 8;

function InternetGetConnectedState(lpdwFlags: LPDWORD;
  dwReserved: DWORD): BOOL; stdcall; external 'WININET.DLL';

function  _IsConnectedToInternet: Boolean;
var
  dwConnectionTypes: Integer;
begin
  try
    dwConnectionTypes := INTERNET_CONNECTION_MODEM +
                         INTERNET_CONNECTION_LAN +
                         INTERNET_CONNECTION_PROXY;
    if InternetGetConnectedState(@dwConnectionTypes, 0) then
      Result := true
    else
      Result := false;
  except
    Result := false;
  end;
end;

 
 
Best Regards

Cesario
Hi guys,

I tried Cesario's solution but still didnt work. Im guessing but I think the InternetGetConnectedState only works when you dont have a physical IP address. Because the cablemodem assigns an IP to my server, Cesario's code sees all the time Internet as available.

I did this test... I connected myself to the Internet with a dial up modem. Cesario's code worked fine. The reason, I guess, is because when Im not connected, no IP is still assigned. Am I right?

So in my opinion, the only way is to ping some server outside the local network to see if there is some activity. For example, trying to ping some DSN server, because they are almost available always...

What do you think? Any other idea?

best regards
Manuel Lopez (lopem)
Hi Manuel,

ping server or GET HTTP page reliable (and universal) but not very fast way.
Ping with 500msec timeout preferred way, it works much faster then HTTP GET in case you have not connected to internet.

-------
Igor.
Igor,

I just installed the Piette's components and used the Ping command as you said. It is not very fast but it works
reliably...

Thanks a lot.
best regards
Manuel Lopez (lopem)
I have Piette's components also...what does the code look like?