C ++ obtains computer host information (computer name, IP address)

Source: Internet
Author: User

[Cpp]
# Include <iostream>
# Include <string>
# Include <vector>
# Include <atlstr. h>
# Include "winsock2.h"
# Pragma comment (lib, "ws2_32.lib ")
Using namespace std;
Int main (int argc, char * argv [])
{
CString m_IP; // temporary IP address variable
CString m_LocalIP;
// Obtain the name of the Local Computer
Char PCnameBuffer [128];
CString m_PCname;
// Initialization: If Initialization is not performed, the following code cannot be executed:
WSAData data;
If (WSAStartup (MAKEWORD (1,1), & data )! = 0)
{
Cout <"initialization error, unable to obtain host information..." <endl;
}
Else
If (0 = gethostname (PCnameBuffer, 128 ))
{
M_PCname = PCnameBuffer;
// Obtain the local IP Address
Struct hostent * pHost;
Int I;
PHost = gethostbyname (PCnameBuffer); // pHost returns a list pointing to the host.
For (I = 0; pHost! = NULL & pHost-> h_addr_list [I]! = NULL; I ++)
{
LPCSTR psz = inet_ntoa (* (struct in_addr *) pHost-> h_addr_list [I]); // obtain the psz variable pointing to the ip address
M_IP + = psz;
}
M_LocalIP = m_IP;
}
Else
{
Cout <"failed to get host information..." <endl;
}
Return 0;
}

# Include <iostream>
# Include <string>
# Include <vector>
# Include <atlstr. h>
# Include "winsock2.h"
# Pragma comment (lib, "ws2_32.lib ")
Using namespace std;
Int main (int argc, char * argv [])
{
CString m_IP; // temporary IP address variable
CString m_LocalIP;
// Obtain the name of the Local Computer
Char PCnameBuffer [128];
CString m_PCname;
// Initialization: If Initialization is not performed, the following code cannot be executed:
WSAData data;
If (WSAStartup (MAKEWORD (1,1), & data )! = 0)
{
Cout <"initialization error, unable to obtain host information..." <endl;
}
Else
If (0 = gethostname (PCnameBuffer, 128 ))
{
M_PCname = PCnameBuffer;
// Obtain the local IP Address
Struct hostent * pHost;
Int I;
PHost = gethostbyname (PCnameBuffer); // pHost returns a list pointing to the host.
For (I = 0; pHost! = NULL & pHost-> h_addr_list [I]! = NULL; I ++)
{
LPCSTR psz = inet_ntoa (* (struct in_addr *) pHost-> h_addr_list [I]); // obtain the psz variable pointing to the ip address
M_IP + = psz;
}
M_LocalIP = m_IP;
}
Else
{
Cout <"failed to get host information..." <endl;
}
Return 0;
}

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.