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

Posted on 2005-04-18
Last Modified: 2008-01-09

   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, states that mY actual IP is "".

There are tips on how to connect to 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.
Question by:thisguirong
    LVL 15

    Expert Comment

    Are you trying to use TClientSocket and TServerSocket components to try and connect across the internet rather than a local network?
    LVL 12

    Expert Comment

    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('');
      lc_pos := Pos('</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);
    LVL 13

    Expert Comment

    actually the 169 is just a (Microsoft) generic ip meaning there is no network connection
    LVL 3

    Accepted Solution

    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 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.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now