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 );