vc获取计算机名和ip地址的方法_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

vc获取计算机名和ip地址的方法

发布时间:2026-01-18  |  点击率:

本文实例讲述了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(); 
}

希望本文所述对大家的VC程序设计有所帮助。

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598