• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2207
  • Last Modified:

How to use Delphi to get my Actual IP instead of Local IP?


   i'm writing a simple client/server application which can transfer text, using Clientsocket and serversocket. I need to know my IP address before the program could connect, thats where the problem comes. Below is the code to get my LocalIP:

uses WinSock;
Function GetLocalIP: string;
wsaData: TWSAData;
P : PHostEnt;
S : array [0..128] of char;
WSAStartup(MAKEWORD(1,1), wsaData);
GetHostName(@s, 128);
P:= GetHostByName(@s);
Result:= iNet_ntoa(PInAddr(p^.h_addr_list^)^);

The code detect my IP as "".
However, the program wont connect. On further search for my IP,
WhatismyIP.com states that mY actual IP is "".

There are tips on how to connect to WhatismyIP.com and get my actual IP using Tbrowser components. But i dont wish to connect to websites.

Anyone knows how to code in Delphi to get my actual IP? it should be a function, and by just clicking on a button, the actual IP will be shown.
1 Solution
Are you trying to use TClientSocket and TServerSocket components to try and connect across the internet rather than a local network?
Here is a small function, which will take your REAL IP address. It is different, because you may be behind proxy. However, if you use you application from LAN network, you will need ""...


function GetMyRealIP : String;
  HTTP    : TidHTTP;
  text    : String;
  lc_pos  : Integer;
  HTTP := TidHTTP.Create(nil);
    text := HTTP.Get('http://whatismyip.com/');
  lc_pos := Pos('WhatIsMyIP.com</title>', text);
  text   := Copy(text, 1, lc_pos-1);
  lc_pos := Pos('Your IP is', text);
  text   := Copy(text, lc_pos + 10, length(text) - lc_pos + 1);
  Result := Trim(text);
actually the 169 is just a (Microsoft) generic ip meaning there is no network connection
The answer to the question "What is my IP address" can have multiple answers.  When it does have multiple answers, which is correct depends on what you are wanting to use the address for.  The term "Actual IP Address" is almost meaningless.

For example, the system I am using to answer this question currently has at least 10 different IP addresses.  Depending on context, only one of then will actually be usable (and for different contexts it is a different one).

In a system with multiple network interfaces (e.g.: wireless, Dial-up & LAN) then each interface will typically have it's own IP addresss.
For arious virtualising techniques (VMWare, some tunnelling products) you will also have additional IP addresses for each connection.
If you connect via a NAT service (common with gateway routers), then to anyone on the other side of the gateway your address (as seen by a Web Site for example) will be that of the gateway.  Note that multiple NAT servers may be involved!!

I assume you are connecting to the Internet via some form of gateway and that you want to find out what IP address a arbitrary Web server will consider you to be.  In this case, the only reliable way to find out is to use a service such as WhatismyIP.com which will essentially echo back the IP address it thinks your request came from.  

Note that when NAT is involved, the IP address reported will not be unique, but will be common for all systems accessing the network via that server.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now