C ++ obtains the Host Name and Host IP Address

Source: Internet
Author: User
/* Compiling environment: Visual C ++ */# include <stdio. h >#include <winsock2.h> # pragma comment (Lib, "ws2_32.lib") int doit (INT, char **) {char host_name [255]; // obtain the local host name if (gethostname (host_name, sizeof (host_name) = socket_error) {printf ("error % d when getting local host name. \ n ", wsagetlasterror (); return 1;} printf (" host name is: % s \ n ", host_name ); // obtain the corresponding "host" struct hostent * Phe = gethostbyname (H Ost_name); If (PHE = 0) {printf ("yow! Bad host lookup. "); return 1;} // cyclically obtain all the IP addresses of the Local Machine for (INT I = 0; phe-> h_addr_list [I]! = 0; ++ I) {struct in_addr ADDR; memcpy (& ADDR, Phe-> h_addr_list [I], sizeof (struct in_addr); printf ("address % d: % s \ n ", I, inet_ntoa (ADDR);} return 0;} int main (INT argc, char * argv []) {wsadata; if (wsastartup (makeword (1, 1), & wsadata )! = 0) {return 255;} int retval = doit (argc, argv); wsacleanup (); Return retval ;}

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.