Obtain the Host ip address on Ubuntu: # include & lt; stdio. h & gt; # include & lt; stdlib. h & gt; # include & lt; netdb. h & gt; # include & lt; unistd. h & gt; # include & lt; errno. h & gt; # include & lt; linux/limits. h & gt; # include & lt; arpa/inet. h & gt; ma
Obtain the Host ip address on Ubuntu:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Main ()
{
Struct hostent * h;
Char ** p;
Char hostname [PATH_MAX];
Gethostname (hostname, PATH_MAX);
Printf ("hostname = % s \ n", hostname );
H = gethostbyname (hostname );
Printf ("hostname = % s \ n", h-> h_name );
Printf ("hostalias = % s \ n", * (h-> h_aliases ));
// Printf ("hostalias2 = % s \ n", * (++ (h-> h_aliases )));
// Printf ("hostalias3 = % s \ n", * (h-> h_aliases) + 2 ));
Printf ("h_length = % d \ n", h-> h_length );
// Format the IP address
Printf ("hostip = % s \ n ",Inet_ntoa (* (struct in_addr *) (h-> h_addr_list [0]));
}
Use on ubuntuPATH_MAXNeed to introduce# Include
UseInet_ntoaNeed to introduce# Include
This may be different from other versions of linux!