Search code examples
c++winsock

WinSock - get local IP address in long


I'm using c++ and winsock. I need to get ip address of local machine in long. Is there any simple way to do it?


Solution

  • Well, if it is a IPv4 address, you can do with binary operations, since long has 4 bytes

    void ipLong2String(int ipLong, char &ipStr) {
        ipStr.clear();
        ipStr << ((ipLong & 0xF000) >> 12) << "." 
                << ((ipLong & 0x0F00) >> 8) << "." 
                << ((ipLong & 0x00F0) >> 04 << "." 
                << (ipLong & 0x000F);
    }
    

    With IPv6 won't be possible.