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?
 
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
 
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
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.