核心代码:
//取得本机IP,在状态栏中显示
char szHostName[MAX_PATH] = {0};
::gethostname(szHostName, MAX_PATH);
hostent *pHost = gethostbyname(szHostName);
if (pHost != NULL)
{
CString strIP;
in_addr* addr = (in_addr*)*pHost->h_addr_list;
strIP.Format("本机IP:%s",inet_ntoa(addr[0]));
m_bar.SetText(strIP, 0, 0);
}
但是 在核心代码之前要有:
//初始化套接字
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2,0);
::WSAStartup(wVersionRequested, &wsaData);
在核心代码之后要有:
//释放套接字
::WSACleanup();