Using XE4. Has anyone had a successful function/unit/code to see if a computer has access to the Internet?

Comments

  1. There are all bad tricks to test if a computer is a connected to the internet. Only one is good: doing an actual connection to several remote sites and see if it works.
    To do the connections, all you need is a socket component to establish the connection. There is no need to transfer data, just connect and then disconnect. The protocol is of no importance since no data has to be transferred.
    Using ICS TWSocket, you would code something like:
    procedure TForm1.ButtonClick(Sender : TObject);
    begin
    WSocket1.Proto := 'TCP';
    WSocket1.Port := '80';
    WSocket1.Addr := 'www.embarcadero.com';  // No 'http://' !!
    WSocket1.OnSessionConnected := WSocket1SessionConnected;
    WSocket1.Connect;
    end;
    procedure TForm1.WSocket1SessionConnected(Sender : TObject; ErrCode:Word);
    begin
        if ErrCode = 0 then begin
            Memo1.Lines.Add('You are connected';
            WSocket1.Close;
        else begin
            Memo1.Lines.Add('Unable to connect');
        end;
    end;

    btw: Just typed the code here. Maybe some typo.

    --
    http://francois-piette.blogspot.be

    ReplyDelete
  2. Thank you. I think with the various snippets I have been trying, I'm in agreement with you regarding lots of bad ways.

    I haven't used ICS in several years, so I'll grab that and install into XE4 and XE5 and try that. It makes sense to me.

    ReplyDelete
  3. Some computers are connected to the internet using a proxy. I that case, you need the parameters to feed into TWSocket properties. Getting the proxy parameters is not easy. Some computers have Internet Explorer configured for the proxy, some other don't because they use another browser. In any case, you have to peek into the correct browser configuration to get the parameters without asking the user.

    ReplyDelete
  4. Only way I can think of would be to try resolving a name, with windows 7 there should be an API to test as it knows if you have a network and no internet connection but I bet they don't share that with us

    ReplyDelete
  5. Alexey Petushkov This is related to IE (WinINet is underlaying layer for IE). Computer may have access to the internet while WinINet can't.

    ReplyDelete
  6. I had actually tried the MSDN solution and it doesn't work all the time. I think François explanation is why.

    ReplyDelete
  7. I solved a similar issue for finding out if a industrial computer on a forklift has connection to the LAN, by building a UDP responder into our server , and having the client do "pings" to the server. I used UDP to avoid the timeout issues related to TCP. This probably is not a feasible method for detecting Internet connectivity, unless you have a reliable UDP responder of your own somewhere.

    ReplyDelete
  8. Why do you need to know if there is internet connectivity? Isn't the best way just to try to connect for whatever 'real' purpose, then gracefully handle a connection failure?

    ReplyDelete

Post a Comment