Getting IP address of local machine

Hi friends,
I’d like to share a simple code snippet I wrote to get the list of local IP addresses of local computer. It works by parsing the output of ifconfig (or ipconfig for windows). Not the most elegant method maybe but it works :slight_smile:

vector<string> ofApp::getLocalIPs()
{
    vector<string> result;
    
#ifdef TARGET_WIN32
    
    string commandResult = ofSystem("ipconfig");
    //ofLogVerbose() << commandResult;
    
    for (int pos = 0; pos >= 0; )
    {
        pos = commandResult.find("IPv4", pos);

        if (pos >= 0)
        {
            pos = commandResult.find(":", pos) + 2;
            int pos2 = commandResult.find("\n", pos);

            string ip = commandResult.substr(pos, pos2 - pos);

            pos = pos2;

            if (ip.substr(0, 3) != "127") // let's skip loopback addresses
            {
                result.push_back(ip);
                //ofLogVerbose() << ip;
            }
        }
    }

#else
    
    string commandResult = ofSystem("ifconfig");
    
    for(int pos = 0; pos >= 0; )
    {
        pos = commandResult.find("inet ", pos);
        
        if(pos >= 0)
        {
            int pos2 = commandResult.find("netmask", pos);
            
            string ip = commandResult.substr(pos+5, pos2-pos-6);
            
            pos = pos2;
            
            if(ip.substr(0, 3) != "127") // let's skip loopback addresses
            {
                result.push_back(ip);
                //ofLogVerbose() << ip;
            }
        }
    }
    
#endif
    
    return result;
}
2 Likes

Nice!

You can also find a suite of cross-platform network utilities in ofxNetworkUtils here (including local ip address, public ip address, etc).

yes, that’s the first thing I tried actually, but the addon gave me a few errors (OSX 10.12.4., XCode 8.3, OF 0.9.8) and since I was in a hurry I had to find a different solution.

Sorry I can’t remember now what was failing

Hm. It’s up to date for openFrameworks master - perhaps it isn’t working for 0.9.8. Sorry it didn’t work for you :frowning: I know that can be frustrating.

I just tested it and the stable branch is working with 0.9.8 and the master branch is working with the master branch of openFrameworks. Not sure what problem you ran into – perhaps you were trying to use the master of the addon with the stable branch of openFrameworks?

that’s very likely, I pulled ofxNetworkUtils master from GIT

Hm, it’s a common problem and I’m trying to come up with a better solution to help (it mentioned it in the README, but sometimes it isn’t clear). I just added some preprocessor directives to this addon to alert people if their versions are out of sync. Maybe it will help.

Anyway, thanks for the feedback and glad you found a solution anyway.

Now I downloaded the stable branch, but the compiler complains about “No member named ‘Net’ in namespace ‘Poco’”

XCode 8.3, OSX 10.12, of 0.9.8

I also have the same problem as @davidemania. It complains about the Net in ‘Net’ in namespace ‘Poco’"