Verify that PC is connected to Internet in Delphi programming

I am looking for an easy way to verify that the PC where my program is running is connected to the Internet.
The program is mine and coded in Delphi XE
Thanks
LVL 1
LeTayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
it's impossible to get a 100% sure answer

we have zones, like between 2 wifi hotspots, in which a return answer never comes, or it takes a very long time
if you are in such a zone, you need to set a timeout

so it's either:
you got a timeout
you're connected to the network
your're not connected to the network

and then comes the next question
do you have privileges to connect to the site on internet  ?
0
sarabandeCommented:
you may use a function like

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(0, nil, 'cmd.exe', '/C ping www.google.com > out.txt', nil, SW_HIDE);
  Sleep(1000);
  Memo1.Lines.LoadFromFile('out.txt');
end;

Open in new window


then parse the lines and check whether there are timeouts or not. you may alter the target address to any other address (also ip addresses like 8.8.8.8) where you know that they don't block ping.

Sara
0
Geert GOracle dbaCommented:
it doesn't mean that when a ping works, that you have access to a page or vice versa

it's better to catch the errors
and evaluate them
function GetPage(aURL: string): string;
var
  Response: TStringStream;
  HTTP: TIdHTTP;
begin
  Result := '';
  Response := TStringStream.Create('');
  try
    HTTP := TIdHTTP.Create(nil);
    try
      try 
        HTTP.Get(aURL, Response);
        // check reponse.code etc ... 
      exception 
        on E: Exception do 
          // handle the errors 
          // you might get timeouts here too 
      end;
    finally
      HTTP.Free;
    end;
  finally
    Response.Free;
  end;
end;

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

LeTayAuthor Commented:
Seems very complicated !
Assume I know a web site that should be up 99.999 % of the time
How shall I code  then ?
0
Geert GOracle dbaCommented:
it is complicated :(
it all depends on the company using your software and what rules they have in place

check the docwiki, it's all about that:
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_an_HTTP_Client
0
LeTayAuthor Commented:
Not companies
Users are at home, connected or outside with portables PC without connection
It is not a problem if the function I am look at returns that the Internet is not accessible when it is.
It is more important that it says the Internet is accessible when it is really accessible
So ?
0
Geert GOracle dbaCommented:
well , if you run the function above

HTTP.Get(aURL, Response);
if Response.code = 200 then 
begin
  // the internet has returned the answer !
  Result := Response.DataString;
end;

Open in new window


test := GetPage('http://yoururl.yoursite');
if test <> '' then ShowMessage('The internet works');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark LewisCommented:
Check out www.nutpile.com. You can run their software on that machine and get notifications about outages and a whole lot more.
We use it as a low cost way of knowing when customer sites are experiencing problems.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.