Obtain the Host ip address on Ubuntu

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.