?
Solved

How to know if I am connected to the internet?

Posted on 2002-05-17
11
Medium Priority
?
241 Views
Last Modified: 2010-04-04
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)
0
Comment
Question by:lopem
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7017589
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;
0
 
LVL 3

Author Comment

by:lopem
ID: 7017997
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)
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 7018048
Hi lopem,

try to ping any well known server's IP oustide of your loacal network. Let say IP of your provider. To make it easy, use F.Piette's TPing component;

e.g.

  Ping1.Address := '192.168.168.4';
  Result := Ping1.Ping = 1;

-------
Igor.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7018085
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 :(
0
 
LVL 9

Expert Comment

by:ITugay
ID: 7018095
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.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7018099
yes Igor, I meant that
some adiministation closed the ports, so I think the second suggestion will be better, right
0
 
LVL 8

Expert Comment

by:Cesario
ID: 7018248
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
0
 
LVL 3

Author Comment

by:lopem
ID: 7019203
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)
0
 
LVL 9

Expert Comment

by:ITugay
ID: 7020879
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.
0
 
LVL 3

Author Comment

by:lopem
ID: 7020888
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)
0
 
LVL 5

Expert Comment

by:Tom Knowlton
ID: 7951455
I have Piette's components also...what does the code look like?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question