CodePudding user response:
Char hostname [256]={0 x00};Gethostname (hostname, sizeof (hostname));//get the machine host name
Hostent * phostent=gethostbyname (hostname);//according to the host name for the machine address list
Hostent& He=* phostent;
Sockaddr_in sa;
for(int i=0; He. H_addr_list [I]; I++)//read the address list and display
{
Memcpy (& amp; Sa. Sin_addr. S_addr, he h_addr_list [I], he. H_length);
Printf (" % d] [IP=% s \ n ", I, inet_ntoa (sa) sin_addr));
}