Get the native IP address in C + + under Windows

Source: Internet
Author: User

BSTR camutilsctrl::get_term_ip (void)
{
Afx_manage_state (AfxGetStaticModuleState ());

CString strresult;

TODO: Add the Dispatch handler code here
Wsadata Wsadata;
WORD sockversion = Makeword (2,2);
Initializing the socket environment
if (:: WSAStartup (sockversion,&wsadata)!=0)
{
Tracelog3 (Log_info, "WSAStartup failed. Errorcode=%d ", GetLastError ());
Exit (0);
}

Get host Name
Char szhost[256]={0};
:: GetHostName (szhost,256);
Tracelog3 (Log_info, "host name =%s", Szhost);
Get the network information under the host name
Hostent *phost =:: gethostbyname (Szhost);//Returns a pointer to the hostent structure that contains host name and address information for the specified host name
/*struct hostent
{
Char *h_name;//official host name
char * * h_aliases;//host Alias
Short h_addrtype;//host IP address type: ipv4-af_inet
Short h_length;//host I ticket address byte length, IPV4 is 4 bytes, or 32 bits
char * * h_addr_list;//host's IP address list
};*/

Processing the acquired network information
IN_ADDR addr;
CString Strtmp,strbuf;
for (int i=0;; i++)
{
Char *p = phost->h_addr_list[i];
if (p==null)
Break
memcpy (&addr. S_un. S_addr,p,phost->h_length);
Char *SLZP =:: Inet_ntoa (addr);//convert sin_addr stored IP (value) to string form (127.0.0.1)
Strbuf = SLZP;
Strtmp.append ((strbuf+ "|"));
}
Shutting down the socket environment
:: WSACleanup ();
Strtmp.trimright ("|");
Evtfsnmessage (strtmp);
strresult = strtmp;

return strresult.allocsysstring ();
}

Get the native IP address in C + + under Windows

Related Article

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.