Get User IP address

So, you need a way to get your user local IP address? Here is the winsock way.
Drop a Button and two Labels then add the bellow code on your window events:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #include <System.hpp>
    #include <Winsock2.h>

    //-----------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
       WORD wVersionRequested;
       WSAData wsaData;
       wVersionRequested = MAKEWORD(1,1);
       WSAStartup(wVersionRequested,&wsaData);
    }
    //-----------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        char Buf[256]={0};
        hostent *p=gethostbyname(Buf);
        //user ip
        Label1->Caption=(inet_ntoa)(*((in_addr*)p->h_addr_list[0]));
        //user computer name
        Label2->Caption=p->h_name;
    }

    //-----------------------------------------------------------------

    void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
        WSACleanup;
    }

    //-----------------------------------------------------------------

Related links:

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>