////////////////////////////////////////////////////////////////////////////////////////
//리턴값
//0 : 연결되어 있지 않았을 경우
//1 : 공인IP주소
//2 : 사설IP주소
////////////////////////////////////////////////////////////////////////////////////////


#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")

int CSendDescDlg::GetIP(CString &strIP)
{
    char szHostName[128];
    struct hostent *pHost;
 
    int nPrivate    = -1;
 
 
    if ( gethostname(szHostName, 128)!=0 )
        return 0;
 
    pHost    = gethostbyname(szHostName);
 
    for (int i=0; pHost->h_addr_list[i]; i++)
    {
        switch ( ((PUCHAR)pHost->h_addr_list[i])[0] )
        {
   // 자동 개인 ip 주소 제거
        case 169:
            if ( ((PUCHAR)pHost->h_addr_list[i])[1]==254 )
                continue;
            break;
   
   // 사설 ip 주소인 경우 저장
        case 10:
            nPrivate    = i;
            break;
        case 172:
            if ( ((PUCHAR)pHost->h_addr_list[i])[1]>15 &&
    (pHost->h_addr_list[i])[1]<32 )
                nPrivate    = i;
            break;
        case 192:
            if ( ((PUCHAR)pHost->h_addr_list[i])[1]==168 )
                nPrivate    = i;
            break;
   
   // 공인 ip 주소인 경우
        default:
   strIP.Format("%d.%d.%d.%d"
    , ((PUCHAR)pHost->h_addr_list[i])[0]
    , ((PUCHAR)pHost->h_addr_list[i])[1]
    , ((PUCHAR)pHost->h_addr_list[i])[2]
    , ((PUCHAR)pHost->h_addr_list[i])[3]);
   return 1;
        }
    }
 
    if (nPrivate>=0)
    {
        strIP.Format("%u.%u.%u.%u"
            , ((PUCHAR)pHost->h_addr_list[nPrivate])[0]
            , ((PUCHAR)pHost->h_addr_list[nPrivate])[1]
            , ((PUCHAR)pHost->h_addr_list[nPrivate])[2]
            , ((PUCHAR)pHost->h_addr_list[nPrivate])[3]);
        return 2;
    }
    else
        return 0;
 
}

Posted by 띠깜
,