Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ishan

Get my IP?

This topic is 5326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know how to get the local computer''s IP? I need a code example that produces the local computer''s IP using sockets in c++ on a linux machine. I''ve found this one example reprinted several times on the internet:
int doit(int, char **)
{
    char ac[80];
    gethostname(ac, sizeof(ac));
    
    cout << "Host name is " << ac << "." << endl;

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Yow! Bad host lookup." << endl;
        return 1;
    }

    for (int i = 0; phe->h_addr_list != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
    }
    
    return 0;
}

int main(int argc, char *argv[])
{

    int retval = doit(argc, argv);

    return retval;
}
 
But I only get 127.0.0.1, even though eth0 is has a different IP. Anyone want to help me out?

Share this post


Link to post
Share on other sites
Advertisement
noticed that the code I posted didn't help you... sorry

[edited by - doho on March 15, 2004 1:02:27 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
get somebody else to tell you your ip..

Share this post


Link to post
Share on other sites
i use this, dunno if it does the same, but it works fine here..
dunno if you can use it, but its worth a try...


//------------

// IP SHOW/LOG

//------------

int showlocalip()
{
char szHostName[128];
char ip[16] = "\0";
struct hostent *pHost;
int i;

if(gethostname(szHostName, 128) == 0)
{
pHost = gethostbyname(szHostName);
for(i=0; i<pHost->h_length; i++)
{
if(i > 0)
sprintf(ip, "%s.", ip);
sprintf(ip, "%s%u", ip, (unsigned int)((unsigned char *)pHost->h_addr_list[0])[i]);
}
iplog("Update IP :",ip);
}
return 0;
}


dont worrie about the iplog thing, its just some logging on my server.

you should replace iplog("......"); with

cout << "Address : " << ip << endl;

[edited by - Bmsfx on March 17, 2004 9:18:55 PM]

Share this post


Link to post
Share on other sites
You really can''t get "the" IP address of your server.

The reason is that a machine may have more than one IP address; either by having more than one network device/interface, or by configuring more than one IP peer on a single interface.

The "real" way to get the IP addresses of your host is to use host-specific device configuration discovery to figure out what the interfaces are; there is no good "portable" way.

For example, "mygateway.mydomain.dom" may resolve to 10.10.1.1 if I''m inside a NAT network, but 123.234.45.56 if I''m outside the network. It''s undefined whether you''re considered "inside" or "outside" the network when you resolve on the gateway machine -- but typically, you''ll be considered "inside".

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!