IPv4에서만 사용


IP 문자열 -> 정수


inline UINT32 ConvertIP(const char* szIP)
{
    SOCKADDR_IN Addr;
    memset(&Addr, 0, sizeof(SOCKADDR_IN));

    unsigned int nIP = inet_addr(szIP);
    if (nIP != INADDR_NONE)
    {
        memcpy(&(Addr.sin_addr), &nIP, sizeof(UINT32));
    }
    else
    {
        HOSTENT* pHostInfo = gethostbyname(szIP);
        if (pHostInfo == NULL) return INADDR_NONE;

        memcpy((char FAR *)&(Addr.sin_addr), pHostInfo->h_addr, pHostInfo->h_length);
    }

    return static_cast<UINT32>(Addr.sin_addr.S_un.S_addr);
}




IP 정수 -> IP 문자열


void NumberToIPString( const UINT32 nIP, const INT32 nMaxIPStrLen, char* szIP )
{
    SOCKADDR_IN Addr;
    memset(&Addr, 0, sizeof(sockaddr_in));

    Addr.sin_addr.S_un.S_addr = nIP;
    strcpy_s( szIP, nMaxIPStrLen, inet_ntoa(Addr.sin_addr) );
}



저작자 표시
신고
by 흥배 2012.09.19 09:00
| 1 |