How to get computer IP address using Delphi software through Socket API?

Using Socket API in Delphi
image_pdfimage_print

Dear reader of SoftMaker.kz blog. As you may know, today many people want to have access to local network computers (LAN) or Internet. Also, many would like to know how to write programs in order to work in a local network (LAN) or Internet. One post from this series has already explained, How to get external IP address using Delphi software. Today we will consider another one of the most interesting tasks; this is getting an IP address of a computer connected to a local network (LAN).

The whole Internet is based on TCP/IP connection. A TCP part describes how two computers can connect to each other to transfer data. An IP part is responsible for transfer and routing of messages between Internet station. Every computer has unique IP address that allows others to know a path to any computer in a network.

In order to get an IP address of a computer that you use to connect to the local network (LAN) we need to call some API functions determined in Winsock module. We will create function GetIPFromHost, that calls several Winsock API functions to get IP address.

Prior to using WinSock function, we have to open a session. The session is open using WSAStartup function. At the end of GetIPFromHost function, SACleanup function is called in order to stop the use of API Windows sockets. In order to get an IP address of a computer connected to a local network (LAN) we have to use GetHostByName function combined with GetHostName function. Every computer is a host, and we can get computer’s name by using GetHostName function. Now having got the computer’s name we will get its IP address using GetHostByName function.

Open Delphi and put one button and two Edit fields on the form. A code given below is required to get IP address that is used by your computer.

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 := 'Socket does not respond!"'; 
    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; 

Write in the event handler of the button the following:

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; 

Get IP

Roughly the same dialogue appears when GetExternalIP – a program for getting external IP address is run.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *