Solved

How to know if I am connected to the internet?

Posted on 2002-05-17
11
236 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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