[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;
}