function to iterate through the IP addresses of the LAN network of current machine.

shaneholmes
shaneholmes used Ask the Experts™
on
The function should get the LAN Ip address from current machine, and iterate through all the IP addresses of the LAN network the current machine is part of.....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
At the top of this page: http://www.torry.net/networks.htm there is a unit to get lists of computer names and IP addresses.

Geoff M.
Here is a function that returns a StringList with all the IPs of the local machine:

function LocalIPs: TStrings;
  type
    PPInAddr = ^PInAddr;
  var
    wsaData: TWSAData;
    HostInfo: PHostEnt;
    HostName: array[0..255] of char;
    Addr: PPInAddr;
  begin
    Result:= TStringList.Create;
    if WSAStartup($0102, wsaData) <> 0 then Exit;
    try
      if gethostname(HostName, SizeOf(HostName)) <> 0 then Exit;
      HostInfo:= gethostbyname(HostName);
      if HostInfo = nil then Exit;
      Addr:= Pointer(HostInfo^.h_addr_list);
      if (Addr = nil) or (Addr^ = nil) then Exit;
      Result.Add(inet_ntoa(Addr^^));
      Inc(Addr);
      while Addr^ <> nil do
      begin
        Result.Add(inet_ntoa(Addr^^));
        Inc(Addr);
      end;
    finally
      WSACleanup;
    end;
  end;

I didn't write this, I got it off the 'net somewhere, unfortunately I cannot remember where.  Thanks to whoever wrote it.

David

Commented:
Check out the libraries available at overbyte.delphicenter.com

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial