You are here: Home » Builder » C++ » [ En ] » Get User IP address

Get User IP address

16 June |

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:

You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Filed under

Builder, C++, [ En ]

| Tags:

Comments are closed.