Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Feb : Help with code converstion - gethostbyname.

www.cryer.info
Managed Newsgroup Archive

Help with code converstion - gethostbyname.

Subject:Help with code converstion - gethostbyname.
Posted by:"mataus" (mata..@gmail.com)
Date:Sat, 23 Feb 2008 14:05:17

Hi all - not a delphi / pascal so please excuse my stupidity.

I'm trying to impliment a DNS lookup with gethostbyname() and I want to
access the multiple IP addresses returned.  I've tested this in BCB and it
seems easy but in Delphi I can't get my code to work.  In BCB I do something
like this:

     struct hostent *remoteHost;
     remoteHost = gethostbyname(host_name);
     struct in_addr addr;
     for (int i = 0; remoteHost->h_addr_list[i] != 0; ++i)
     {
        memcpy(&addr, remoteHost->h_addr_list[i], sizeof(struct in_addr));
        printf("\tAddress %s
", inet_ntoa(addr));
     }

however, in Delphi, my test for the last entry in the h_addr_list does not
seem to compile - complaining about in compatible data types.  My Delphi
code looks like this:

procedure TForm1.Button1Click(Sender: TObject);
var
   result : PHostEnt;
   WSAData: TWSAData;
   iIPAddr : Cardinal;
   i : integer;

begin
   if WSAStartUp(WSVersion, WSAData) = 0 then // yes, Winsock does exist ...
   try
      result := gethostbyname('www.yahoo.com');
      if (result <> nil) and (result^.h_addrtype = AF_INET) then
      begin
         i := 0;
         while (result^.h_addr_list^[i] <> '0') do
         begin
            move(result^.h_addr_list^[i], iIPAddr, sizeof(iIPAddr));
            ShowMessage('got it: ' + IntToStr(iIPAddr));
            i := i + 1;
         end
      end;
   finally
      WSACleanUp;
   end
end;

my while look just keeps on going... Can anyone point me in the right
direction here?

Many thanks in advance,

Mike C

Replies:

www.cryer.info
Managed Newsgroup Archive