VC obtains the IP address and Host Name of the machine.

Source: Internet
Author: User

Using WinSock to obtain the Host Name and IP address is actually very simple. The main functions used are gethostname and gethostbyname.

First, use gethostname (char * Name, int namelen) to obtain the host name. Name is the Buf that saves the host name, And namelen is the length of the specified Buf. Then, use gethostbyname (char * ADDR) to obtain the Host IP address. The return value of this function is the hostent pointer. The hostent struct is as follows:

Struct hostent {
Char far *{
Showtip (this)
} "> H_name;
Char far *{
Showtip (this)
} "> H_aliases;
Short {
Showtip (this)
} "> H_addrtype;
Short {
Showtip (this)
} "> H_length;
Char far *{
Showtip (this)
} "> H_addr_list;
};

Obtain the Host IP Address by traversing h_addr_list.

 

Char * pchhostname = new char [256]; <br/> gethostname (pchhostname, 256); <br/> hostent * phost = gethostbyname (pchhostname ); <br/> int I; <br/> cstring strip; <br/> for (I = 0; phost! = NULL & phost-> h_addr_list [I]! = NULL; I ++) <br/>{< br/> lpctstr psz = inet_ntoa (* (struct in_addr *) phost-> h_addr_list [I]); <br/> strip + = psz; <br/> strip + = ". "; <br/>}< br/> This-> getdlgitem (idc_zhuji)-> setwindowtext (pchhostname); <br/> This-> getdlgitem (idc_ip) -> setwindowtext (strip );

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.