1.获得主机名
//获得主机名
gethostname(szHostName, 128);
CString str;
for(int i = 0; i <= 128; i++)
{
if(szHostName[i] != '/0')
str.Insert(i,szHostName[i]);
if(szHostName[i] == '/0')
{
MessageBox(_T("OUT"));
MessageBox(str);
break;
}
}
2.获得IP地址
struct hostent* pHost;
//获得IP地址
pHost = gethostbyname(szHostName);
for(i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
char str[100];
char addr[20];
int j;
LPCSTR psz = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]);
m_IP.AddString(psz);
} by《VC网络编程》