# VBForums CodeBank > CodeBank - Other >  [DELPHI] - Get IP Address

## Madboy

```
uses
  Winsock;

function getIPs: Tstrings;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := TstringList.Create;
  Result.Clear;
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I    := 0;
  while pPtr^[I] <> nil do
  begin
    Result.Add(inet_ntoa(pptr^[I]^));
    Inc(I);
  end;
  WSACleanup;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines := GetIps;
end;
```

----------


## Madboy

There is an alternative way to do the above, this method (found from delphi.about.com) shows how to retrieve IP Address and Host name.

You may visit the exact link here: http://delphi.about.com/od/networking/l/aa103100a.htm 

or view the below:



```
uses Winsock; 

function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean; 
type 
  Name = array[0..100] of Char; 
  PName = ^Name; 
var 
  HEnt: pHostEnt; 
  HName: PName; 
  WSAData: TWSAData; 
  i: Integer; 
begin 
  Result := False;     
  if WSAStartup($0101, WSAData) <> 0 then begin 
    WSAErr := 'Winsock is not responding."'; 
    Exit; 
  end; 
  IPaddr := ''; 
  New(HName); 
  if GetHostName(HName^, SizeOf(Name)) = 0 then
  begin 
    HostName := StrPas(HName^); 
    HEnt := GetHostByName(HName^); 
    for i := 0 to HEnt^.h_length - 1 do 
     IPaddr :=
      Concat(IPaddr,
      IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.'); 
    SetLength(IPaddr, Length(IPaddr) - 1); 
    Result := True; 
  end
  else begin 
   case WSAGetLastError of
    WSANOTINITIALISED:WSAErr:='WSANotInitialised'; 
    WSAENETDOWN      :WSAErr:='WSAENetDown'; 
    WSAEINPROGRESS   :WSAErr:='WSAEInProgress'; 
   end; 
  end; 
  Dispose(HName); 
  WSACleanup; 
end;

procedure TForm1.Button1Click(Sender: TObject); 
var 
  Host, IP, Err: string; 
begin 
  if GetIPFromHost(Host, IP, Err) then begin 
    Edit1.Text := Host; 
    Edit2.Text := IP; 
  end 
  else 
    MessageDlg(Err, mtError, [mbOk], 0); 
end;
```

----------


## Philips10

I get the IP of the computer I am working on ONLY
(2 computer with a Router and a broadband internet).

What am I doing wrong.

----------


## Pino

nothing the code only returns the local ip of the machine. for your internet Ip, use

www.WhatIsMyIp.com

----------


## Philips10

Thank you

----------


## set4s

can anyone tell me, how to get the outside pc ip, not the local one? i think it should be something with getting output from a website that provides showing the ip and then exctracting an ip address from the http response... can anyone give a bit of advice ? :]

----------

