手机
当前位置:查字典教程网 >编程开发 >C语言 >vc获取计算机名和ip地址的方法
vc获取计算机名和ip地址的方法
摘要:本文实例讲述了vc获取计算机名和ip地址的方法。分享给大家供大家参考。具体实现方法如下:#include#include#pragmacom...

本文实例讲述了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("%sn",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("%sn",buf); } WSACleanup(); }

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

【vc获取计算机名和ip地址的方法】相关文章:

使用root权限运行自己所编译程序的解决方法

解决C++中重定义的方法总结

atoi和itoa函数的实现方法

用c 获取文件MD5值的实现方法

随机加密程序的实现方法

C++实现两个日期间差多少天的解决方法

C++实现正态随机分布的方法

C++获取zip文件列表方法

求子数组最大和的解决方法详解

解析如何在C语言中调用shell命令的实现方法

精品推荐
分类导航