Solved

How to know if I am connected to the internet?

Posted on 2002-05-17
11
233 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:mnasman
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 50 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
 
LVL 22

Expert Comment

by:mnasman
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:mnasman
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:knowlton
ID: 7951455
I have Piette's components also...what does the code look like?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now