主要是两个函数的使用,gethostname();、gethostbyname();
自定义两个函数GetLocalHostName获取计算机名、GetIPAddress获取IP地址
int CIPDlg::GetLocalHostName(CString &strHostName)
{
char szHostName[];
int nRetCode;
nRetCode = gethostname(szHostName, sizeof(szHostName));
if(nRetCode != )
{
strHostName = _T("Not available");
return WSAGetLastError();
}
strHostName = szHostName;
return ;
}
int CIPDlg::GetIPAddress(const CString &strHostName, CString &strIPAddress)
{
struct hostent FAR *lpHostEnt = gethostbyname(strHostName);
if(lpHostEnt == NULL)
{
strIPAddress = _T("");
return WSAGetLastError();
}
LPSTR lpAddr = lpHostEnt->h_addr_list[];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr, lpAddr, );
strIPAddress = inet_ntoa(inAddr);
if(strIPAddress.IsEmpty())
{
strIPAddress = _T("Not avilable");
}
}
return ;
}