gethostbyname的2种用法得到IP(字符型和bigendian整数型)

BOOL DomainToIP(char *Domain,char *toIP)  //由域名得到IP地址 
 { 
 	HOSTENT *host_entry; 
 	host_entry=gethostbyname(Domain); 
 	if(host_entry!=0) 
 	{ 
 		sprintf(toIP,"%d.%d.%d.%d", 
 			(host_entry->h_addr_list[0][0]&0xff), 
 			(host_entry->h_addr_list[0][1]&0xff), 
 			(host_entry->h_addr_list[0][2]&0xff), 
 			(host_entry->h_addr_list[0][3]&0xff)); 
 	} 
 	else 
 	{ 
 		return FALSE; 
 	} 
 	return TRUE; 
 }


void Init(char*hostname,unsigned short port)
	{
		WSAStartup(0x0202,&m_wsa);
		struct hostent* host;
		host =gethostbyname(hostname);
		if(host==NULL)
		{
			MessageBox(NULL,TEXT("unable to resolve host name"),
				TEXT("warning"),MB_OK);
			PostQuitMessage(0);
		}
		m_IP=*((unsigned long*)host->h_addr);
		m_port=htons(port);
		m_addr.sin_port=m_port;
		m_addr.sin_family=AF_INET;
		m_addr.sin_addr.s_addr=m_IP;
	};



你可能感兴趣的:(网络)