C++获取本机地址信息
int main(){
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
printf("WSAStartup failed %s\n", WSAGetLastError());
return -1;
}
char hostname[128];
int res = gethostname(hostname, sizeof(hostname));
if (res != 0) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
printf("hostname=%s\n", hostname);
hostent* pHostent = gethostbyname(hostname);
if (pHostent==NULL)
{
printf("Error: %u\n", WSAGetLastError());
return -1;
}
hostent& host = *pHostent;
sockaddr_in sain;
for (int ilen=0;host.h_addr_list[ilen];ilen++)
{
memcpy ( &sain.sin_addr.s_Sun, host.h_addr_list[ilen],host.h_length);
printf("Address: %s\n", inet_ntoa(sain.sin_addr));
}
WSACleanup();
return 0;
}
空间不能用`借个位置记录下