vc 获取计算机名 和 ip 地址

#include <winsock2.h> 
#include <stdio.h> 
#pragma comment(lib,"ws2_32.lib")

void main() 
{ 
WSADATA wsadata; 
WORD dwVersionRequested; 
int err; 
err=WSAStartup(dwVersionRequested,&wsadata); 
char hostname[128]; 
if(gethostname(hostname,128)==0) 
{ 
   printf("%s\n",hostname);//计算机名字 
} 
char buf[20];
//memset(buf,0,80);
struct hostent *pHost = gethostbyname(hostname);

for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++) 
{   
   //将它放入字符数组中便于应用
   strcpy(buf,inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
   //inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]); //IP地址
   printf("%s\n",buf);
} 

WSACleanup(); 
}

猜你喜欢

转载自blog.csdn.net/daofengdeba/article/details/13503391
今日推荐