Solved

How to know if I am connected to the internet?

Posted on 2002-05-17
11
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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