21天学通c语言百度云:C++builder中如何获取本机IP

来源:百度文库 编辑:中科新闻网 时间:2024/05/03 07:06:30
详细一点 谢谢

请参考一下,下面这个:
#include "winsock2.h"
#pragma comment( lib, "ws2_32" )
const char* get_first_ip(void)
{
char szHostName[128];
const char* pszAddr = NULL;
struct hostent * pHost;
int i;
WSADATA wsaData;
WSAStartup(0x202, &wsaData );
if( gethostname(szHostName, 128) == 0 )
{
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{/*对每一个IP地址进行处理*/
pszAddr = inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
break;
}
}
WSACleanup();
return pszAddr;
}