如何用VC实现从网址中提取所有IP地址的功能?

2026-04-18 22:192阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计295个文字,预计阅读时间需要2分钟。

如何用VC实现从网址中提取所有IP地址的功能?

通过调用gethostbyname系统函数解析IP地址,以下是一个简化版的代码示例:

cvoid GetHostNamebyIp(char *hostName) { if (hostName==NULL) { return; } int WSA_return=0; WSADATA WSAData; HOSTENT *host_entry; char szIP[1024]={0};

AfxMessageBox(hostName); WSA_return=WSAStartup(MAKEWORD(2, 2), &WSAData); if (WSA_return==0) { host_entry=gethostbyname(hostName); if (host_entry !=NULL) { strcpy(hostName, host_entry->h_name); } WSACleanup(); }}

通过调用gethostbyname 系统函数进行解析

void GetHostNamebyIp(char* hostName) { if(NULL == hostName) { return; } int WSA_return = 0; WSADATA WSAData; HOSTENT *host_entry; char szIP[1024] = {0}; AfxMessageBox(hostName); WSA_return=WSAStartup(0x0202,&WSAData); if(0 == WSA_return) { host_entry = gethostbyname(hostName); if(0 != host_entry) { char** ipAddress = host_entry->h_addr_list; for(; *ipAddress != NULL; ipAddress++) { strcpy(szIP,inet_ntoa(*(LPIN_ADDR)*(ipAddress))); WriteIPFile("ip.txt",hostName ,szIP); } } } }

将解析出来的ip地址写入到文件中

//写文件,第一个参数文件名,第二个参数hostName,第三个参数文件内容 void WriteIPFile(char* FileName, char* hostName, char* text) { HANDLE hFILE=CreateFile(FileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if(hFILE==INVALID_HANDLE_VALUE) { //文件创建失败 return; } if(SetFilePointer(hFILE,0,NULL,FILE_END)==-1) { printf("SetFilePointer error\n"); return ; } DWORD dwhostWrite; if(!WriteFile(hFILE,hostName,strlen(hostName),&dwhostWrite,NULL)) { printf("WriteFile error\n"); return ; } if(!WriteFile(hFILE,"\r\n",2,&dwhostWrite,NULL)) { printf("WriteFile error\n"); return ; } DWORD dwWrite; if(!WriteFile(hFILE,text,strlen(text),&dwWrite,NULL)) { printf("WriteFile error\n"); return ; } if(!WriteFile(hFILE,"\r\n",2,&dwWrite,NULL)) { printf("WriteFile error\n"); return ; } CloseHandle(hFILE); }

到此这篇关于VC实现将网址解析出所有ip地址的文章就介绍到这了,更多相关vc实现网址解析ip地址内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

如何用VC实现从网址中提取所有IP地址的功能?
标签:实例

本文共计295个文字,预计阅读时间需要2分钟。

如何用VC实现从网址中提取所有IP地址的功能?

通过调用gethostbyname系统函数解析IP地址,以下是一个简化版的代码示例:

cvoid GetHostNamebyIp(char *hostName) { if (hostName==NULL) { return; } int WSA_return=0; WSADATA WSAData; HOSTENT *host_entry; char szIP[1024]={0};

AfxMessageBox(hostName); WSA_return=WSAStartup(MAKEWORD(2, 2), &WSAData); if (WSA_return==0) { host_entry=gethostbyname(hostName); if (host_entry !=NULL) { strcpy(hostName, host_entry->h_name); } WSACleanup(); }}

通过调用gethostbyname 系统函数进行解析

void GetHostNamebyIp(char* hostName) { if(NULL == hostName) { return; } int WSA_return = 0; WSADATA WSAData; HOSTENT *host_entry; char szIP[1024] = {0}; AfxMessageBox(hostName); WSA_return=WSAStartup(0x0202,&WSAData); if(0 == WSA_return) { host_entry = gethostbyname(hostName); if(0 != host_entry) { char** ipAddress = host_entry->h_addr_list; for(; *ipAddress != NULL; ipAddress++) { strcpy(szIP,inet_ntoa(*(LPIN_ADDR)*(ipAddress))); WriteIPFile("ip.txt",hostName ,szIP); } } } }

将解析出来的ip地址写入到文件中

//写文件,第一个参数文件名,第二个参数hostName,第三个参数文件内容 void WriteIPFile(char* FileName, char* hostName, char* text) { HANDLE hFILE=CreateFile(FileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if(hFILE==INVALID_HANDLE_VALUE) { //文件创建失败 return; } if(SetFilePointer(hFILE,0,NULL,FILE_END)==-1) { printf("SetFilePointer error\n"); return ; } DWORD dwhostWrite; if(!WriteFile(hFILE,hostName,strlen(hostName),&dwhostWrite,NULL)) { printf("WriteFile error\n"); return ; } if(!WriteFile(hFILE,"\r\n",2,&dwhostWrite,NULL)) { printf("WriteFile error\n"); return ; } DWORD dwWrite; if(!WriteFile(hFILE,text,strlen(text),&dwWrite,NULL)) { printf("WriteFile error\n"); return ; } if(!WriteFile(hFILE,"\r\n",2,&dwWrite,NULL)) { printf("WriteFile error\n"); return ; } CloseHandle(hFILE); }

到此这篇关于VC实现将网址解析出所有ip地址的文章就介绍到这了,更多相关vc实现网址解析ip地址内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

如何用VC实现从网址中提取所有IP地址的功能?
标签:实例