获取本地计算机的IP地址
关键点
WSAStartup
The Windows Sockets WSAStartup function initiates use of Ws2_32.dll by a process.
int WSAStartup(
WORDwVersionRequested,
LPWSADATAlpWSAData
);
实现过程
#include <winsock2.h> #pragma comment(lib, "ws2_32.lib") char* GetIpAddress() { WSADATA wsadata; WSAStartup(MAKEWORD(2, 2), &wsadata); char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); hostent *p = gethostbyname(szHostName); return inet_ntoa(*(in_addr *)p->h_addr_list[0]); }
MessageBox(GetIpAddress()); |
图
备注
//szHostName 就是计算机的名子
相关链接