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

· 2006-07-05
点点滴滴 程序设计

Winsock API 取得本地主机的名字及地址

#include <winsock.h>
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();
}
  • 本文作者:恩爸
  • 本文链接:https://www.sirit.com.cn/archives/25/
  • 版权许可:知识共享署名-相同方式共享4.0国际许可协议
  • 友情提示:本文更新于2022年08月12日,已超过1105天没有更新,若内容或图片失效,请留言反馈。
  • 如何播放APE格式音频文件(转自VeryCD)
  • 你相信自己的眼睛吗?
取消回复

说点什么?

本站已经运行0天 © 2005- 大嘴狗狗. - Typecho & Moricolor - 粤ICP备18101800号 - 粤公网安备44030402005553号