#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
struct in_addr g_local_addr;
char g_local_ip[32] = "";
WSADATA wsaData; //指向WinSocket信息结构的指针
if( WSAStartup(MAKEWORD(2, 2 ), &wsaData ) !=0 )//进行WinSocket的初始化
{
a.Format( _T("Can't initiates windows socket, error number: %d"), WSAGetLastError() );
plist->AddString( a );//初始化失败返回-1
return 0;
}
int get_local_addr()
{
char host[64] = "";
int ret;
ret = gethostname(host, sizeof(host));
if( 0 != ret)
{
return 0;
}
struct hostent *p = gethostbyname(host);
if( (NULL == p) || (NULL == p->h_addr_list[0]) )
{
return 0;
}
g_local_addr.S_un.S_addr = *(u_long *)(p->h_addr_list[0]) ;
char *str = inet_ntoa(g_local_addr);
if( str )
{
strcpy(g_local_ip, str);
}
else
{
return 0;
}
return 1;
}