1
\$\begingroup\$

This HostResolve procedure runs on a thread separately, to not interrupt other working functions. I would like to ask here if this procedure is correctly written or not?

Any recommendations and improvements, if any one willing to help?

Procedure HostResolve();

   const
   URL : array[1..4] of String =('facebook.com', 'google.com', 'yahoo.com', 'gmail.com');

   var
      IP:String;
      IsConnected:Boolean;
      i:integer;

   begin

      IsConnected := false;
      While true do
      begin
        if IsConnectedToInternet then
          begin
           if IsConnected = false then
             begin
        //

          for  i := Low(URL) to High(URL) do
            begin
              if  HostToIp(URL[i], IP) then
               begin
                 ShowMessage(IP);
                 IsConnected := true;
                 Break;
               end;
            end;


        //
        end;
      end else begin
          IsConnected := false;
      end;
        Sleep(1000);
    end;
 end;


 Function IsConnectedToInternet: Boolean;
 var
  dwConnectionTypes: DWORD;
   begin
    dwConnectionTypes :=
    INTERNET_CONNECTION_MODEM +
    INTERNET_CONNECTION_LAN +
    INTERNET_CONNECTION_PROXY;
    Result := InternetGetConnectedState(@dwConnectionTypes, 0);
  end;

Function HostToIP(sHost: string; var sIP: string):Boolean;
var
  aHostName: array[0..255] of Char;
  pcAddr   : PChar;
  HostEnt  : PHostEnt;
  wsData   : TWSAData;
begin
  WSAStartup($0101, wsData);
  try
    GetHostName(aHostName, SizeOf(aHostName));
    StrPCopy(aHostName, sHost);
    hostEnt := GetHostByName(aHostName);
    if Assigned(HostEnt) then
      if Assigned(HostEnt^.H_Addr_List) then begin
        pcAddr := HostEnt^.H_Addr_List^;
        if Assigned(pcAddr) then begin
          sIP := Format('%d.%d.%d.%d', [Byte(pcAddr[0]), Byte(pcAddr[1]),
            Byte(pcAddr[2]), Byte(pcAddr[3])]);
          Result := True;
        end else
          Result := False;
      end else
        Result := False
    else begin
      Result := False;
    end;
  finally
    WSACleanup;
  end;
end;
\$\endgroup\$
  • \$\begingroup\$ @Greenonline do not edit code unless you are sure this is due to copy and paste errors. \$\endgroup\$ – Pimgd Feb 19 '15 at 8:12
  • \$\begingroup\$ Ok, sorry. I was just trying to indent so as to make the code a bit more readable. The _begin_s and _end_s didn't line up correctly, and made it a bit hard to read. As were the blank lines. I hoped that it would help someone to provide an answer. :-) \$\endgroup\$ – Greenonline Feb 19 '15 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.