How to get external IP address using Delphi software?

External IP address in WAN
image_pdfimage_print

Dear readers of SoftMaker.kz blog. Today I’ll give a Delphi example that can help you to get external IP address
in the Internet. One post from this series has already explained how to get computer IP address in a local network using Delphi. Here is a Delphi example that can help you to get external IP address even if you work in a local network and your computer is behind proxy server, gateway or router. This functional is realized using a little program GetExternalIP.

Internet Protocol Address allows computers (printers, modems) to interact with each other in a computer network. 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 stations. If your computer is a part of home or office network, you must have equipment for the Internet connection that links home (office) local local network (LAN) with WAN.

As a rule, internal (local) IP addresses are numbers like: “192.68.1.2”. External (global) IP addresses of a modem (for example
KEENETIC router, in russian), are installed when a modem is connected to an Internet provider. If you want to know your external IP address using Delphi code, you may want to use a website “http://www.softmaker.kz”.

First, you need to put a component IdFTP on a form from a component set Indy Clients. A code given below is required to get modem IP address that is used by your computer to connect to the Internet.

function GetHTML(const AURL: string): string;
 var
   sHead,sHost,sPage: string;
   x,xCnt,xCntTotal: integer;
   sock: TClientSocket;
   ws: TWinSocketStream;
   ss: TStringStream;
   buff: array[0..4095] of char;
 const
   CrLf = #13#10;
 begin
   Result := '';
 
   sHost := AURL;
   x := Pos('//',sHost) ;
   if x > 0 then
     System.Delete(sHost,1,x+1) ;
   x := Pos('/',sHost) ;
   if x > 0 then
   begin
     sPage := Copy(sHost,x,Length(sHost)) ;
     System.Delete(sHost,x,Length(sHost)) ;
   end
   else
   begin
     sPage := '/';
   end;
 
   sock := TClientSocket.Create(nil) ;
   try
     try
       sock.ClientType := ctBlocking;
       sock.Port := 80;
       sock.Host := sHost;
       sock.Open;

       // Set up a 20 second delay 
       ws := TWinSocketStream.Create(sock.Socket,20000) ;
       ss := TStringStream.Create('') ;
       try
         sHead := 'GET ' + sPage + ' HTTP/1.0 ' + CrLf + 
				'Host: ' + sHost + CrLf + CrLf;
         StrPCopy(buff,sHead) ;
 
         ws.Write(buff,Length(sHead) + 1) ;
         ws.Position := 0;
 
         FillChar(buff,SizeOf(buff),0) ;
         repeat
           xCnt := ws.Read(buff,SizeOf(buff)) ;
           xCntTotal := xCntTotal + xCnt;
           ss.Write(buff[0],xCnt) ;
         until xCnt = 0;
 
         Result := ss.DataString;
       finally
         ws.Free;
         ss.Free;
       end;
     except
   end;
   finally
     sock.Free;
   end;
 end;

That’s how you can you use this function in order to get external IP address from en.softmaker.kz.

Put a button on the form and write in the event handler the following:

procedure TForm1.Button1Click(Sender: TObject);
var
   ip: string;
 begin
   with TStringlist.Create do
   try
     Text := GetHTML('http://en.softmaker.kz/get_an_external_ip_address.php') ;
     if Count > 0 then ip := Strings[Count - 1];
   finally
     Free;
   end;
   ShowMessage('Your external ip address: ' + ip);
end;

File content get_an_external_ip_address.php is as follows:

<?php

$ip=$_SERVER['REMOTE_ADDR'];
echo $ip;

?>
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 *