랜 카드가 1개 있다는 가정하에서….

#include <ws2tcpip.h>

bool ServerInfoMgr::GetLocalIP( string& strIP )
{
    char host_name[256];
    if (gethostname(host_name, 256) == SOCKET_ERROR)
         return false;

   struct addrinfo hints, *res = NULL;
   char *szRemoteAddress=NULL, *szRemotePort=NULL;
   int rc; memset( &hints, 0, sizeof(hints) );
   hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_protocol = IPPROTO_TCP;

   rc = getaddrinfo( host_name, szRemotePort, &hints, &res );
   if( rc == WSANO_DATA ) { return false; }

   char szIP[MAX_IP_STRING_LENGTH] = {0, };
   sprintf_s( szIP, MAX_IP_STRING_LENGTH, "%d.%d.%d.%d",
               (unsigned char)res->ai_addr->sa_data[2],
              (unsigned char)res->ai_addr->sa_data[3],
              (unsigned char)res->ai_addr->sa_data[4],
              (unsigned char)res->ai_addr->sa_data[5] );
             
   strIP = szIP;
   return true;
}

저작자 표시
신고
by 흥배 2009.12.07 08:30
| 1 |