龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

取得本地internet机器的名字及IP地址

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
一、下面的例子使用 Winsock API 取得本地主机的名字及地址 void __fastcall TForm1::Button1Click(TObject *Sender) { hostent *p; char s[128]; char *p2; //Get the computer name gethostname(s, 128); p = gethostbyname(s); Memo1

  一、下面的例子使用 Winsock API 取得本地主机的名字及地址

  

void __fastcall TForm1::Button1Click(TObject *Sender)

  

{

  

hostent *p;

  

char s[128];

  

char *p2;

  

//Get the computer name

  

gethostname(s, 128);

  

p = gethostbyname(s);

  

Memo1->Lines->Add(p->h_name);

  

//Get the IpAddress

  

p2 = inet_ntoa(*((in_addr *)p->h_addr));

  

Memo1->Lines->Add(p2);

  

}

  

void __fastcall TForm1::FormCreate(TObject *Sender)

  

{

  

Word wVersionRequested;

  

WSADATA wsaData;

  

  

//Start up WinSock

  

wVersionRequested = MAKEWORD(1, 1);

  

WSAStartup(wVersionRequested, &wsaData);

  

}

  

  

void __fastcall TForm1::FormDestroy(TObject *Sender)

  

{

  

WSACleanup();

  

}

  

精彩图集

赞助商链接