VC實現將網址解析出所有ip地址的實例代碼

通過調用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地址內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!