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:
-
#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;
-
}
-
-
//-----------------------------------------------------------------
C++:
Related links:
- Pv6 addresses: RFC 4291
- Ip Address (@ Wikipedia)
related things:
Author: Raz | On June 16th, 2007 | C/C++, C++ Builder, [ En ] | No Comments
Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Spammers: Beware of » my Dog! «




