windows网络编程 实现域名查询ip

发布时间:2025-12-28 点击:8
#include <winsock2.h> #include <ws2tcpip.h> #include <iostream> #include <windows.h> #pragma warning( disable : 4996) #pragma comment(lib, "ws2_32.lib") using namespace std; int main(int argc, char argv) { //—————————————– // declare and initialize variables /* * wsadata结构被用来储存调用afxsocketinit全局函数返回的windows sockets初始化信息。 * 这个结构被用来存储被wsastartup函数调用后返回的windows sockets数据。它包含winsock.dll执行的数据。 */ wsadata wsadata; int iresult; dword dwerror; /*每个word为2个字节的长度,dword 双字即为4个字节,每个字节是8位*/ int i = 0; struct hostent *remotehost; /*域名*/ char *host_name; /*主机名*/ struct in_addr addr; /*ip*/ char palias; //———————注意下面的话———————— // 开始卡在这里卡了好久,这里是检查输入的参数,原来网上的版本是 2 意思是你还得输入一个参.
//就像 get_ip.exe wwwbaidu.com 这里就是两个参数,前面的加上后面的域名 // validate the parameters if (argc != 1) { printf("usage: gethostip hostnamen"); return 1; } // 此处应添加的程序如下 // 1. 当初始化winsocket iresult = wsastartup(makeword(2, 2), &wsadata);/* & 取地址*/ // 2. 检查该socket是否初始化成功,即该socket是否等于0;如果初始化不成功,应当给出错误报警,并结束程序。 if (iresult != 0) { printf("初始化失败!n"); return 1; } /////////////////结束/////////////////////////////////// //host_name = argv[1]; cout << "输入要查询的域名: "; char host[20];//定义一个数组存放输入的域名,并把值传递给 host_name cin >> host; host_name = host; //host_name = "www.baidu.com"; printf("calling gethostbyname with %sn", host_name); // 此处应添加的程序如下 // 1. 利用函数gethostbyname(),获取给定主机名的指针。 remotehost = gethostbyname(host_name); // 2. 应当熟悉该结构指针的结构 // 其中该函数申明如下:struct hostent* gethostbyname(const char *name) // 此处应添加的程序如下 // 1. 如果上面函数返回的主机结构指针为空(null),则做如下处理: // a. 利用函数 int wsagetlasterror ( void ) 检查当前是否发生网络错误, // b. 返回的发生的错误类型并作相应的处理,比如,若没有找到主机的错误(此时该函数返回wsahost_not_found) if (remotehost == null) { dwerror = wsagetlasterror(); if (dwerror != 0) { if (dwerror == wsahost_not_found) { printf("host not foundn"); return 1; } else if (dwerror == wsano_data) { printf("no data record foundn"); return 1; } else { printf("function failed with error: %ldn", dwerror); return 1; } } } else { printf("function returned:n"); printf("tofficial name: %sn", remotehost->h_name); for (palias = remotehost->h_aliases; *palias != 0; palias ) { printf("talternate name #%d: %sn", i, *palias); } printf("taddress type: "); switch (remotehost->h_addrtype) { case af_inet: printf("af_inetn"); break; case af_netbios: printf("af_netbiosn"); break; default: printf(" %dn", remotehost->h_addrtype); break; } printf("taddress length: %dn", remotehost->h_length); i = 0; if (remotehost->h_addrtype == af_inet) { while (remotehost->h_addr_list[i] != 0) { addr.s_addr = *(u_long *)remotehost->h_addr_list[i ]; printf("tip address #%d: %sn", i, inet_ntoa(addr)); } } else if (remotehost->h_addrtype == af_netbios) { printf("netbios address was returnedn"); } } system("pause"); /*防止窗体关闭函数*/ return 0; }
照着书敲的代码愣是改了半天,vs大法好啊,环境就调了半天 把遇到的问题一起上传上来吧 编译器报错c4996 报错的情况通常为: 1 错误 8 error c4996: \\\’sprintf\\\’: this function or variable may be unsafe. consider using sprintf_s instead. to disable deprecation, use_crt_secure_no_warnings. see online help for details. 此时,通常有3种方式可以解决: (1)使用vs提供的 编译器选择性提供warning功能 link: http://msdn.microsoft.com/en-us/library/2c8f766e.aspx例:使用 #pragma warning( disable : 4996) 即可消除。 (2) 设置预处理选项:a. project properties->configuration properties->c/c ->preprocessor-&g

商标注册类别眼镜是多少类
临汾企业邮箱注册需要什么
新云服务器价格走势分析
云服务器配置学习
可相互跳转、批量管理门店 小程序是抓住零售新机会的一把利器吗?
什么是动态域名解析
现在哪家云服务器便宜
域名模版问题-其他问题