Jump to content
  • Advertisement
Sign in to follow this  
cppgirl

solved..closed LAN IP#?

This topic is 4224 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

Hi, how can I get my LAN IP# in linux please (programmatically)? I want a way to do it in preferably C++ code (function/method) but I'd be willing to rely on SDL_net way or Tcl way too but I'd rather a C++ way :) Parsing ifconfig isn't an option because some versions of linux require root level to run ifconfig (as in the version I use slackware). P.S.: I already have a way to do this via wxWidgets in MS Windows and Mac OS X I'm trying to find out how to in Linux (without having to parse ifconfig or netstat!) the wx way isn't working in Linux. I havent been on this site in a while so my merely posting here is because I've tried two other forums with no success and lots of googling :/ This is for my MMORPG!!! ... ( kidding about the last part ^_^ ) [Edited by - cppgirl on March 24, 2007 10:59:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
You can try getifaddrs but most versions of Linux dont have that function (more of a Unix/BSD function)

You can also try calling ioctl with SIOCGIFADDR to get the IP Address but in order for that to work you have to have an open socket.


struct ifreq interface;

//assume socket is an open socket
int results = ioctl(socket, SIOCGIFADDR, &interface);

if(results == 0)
{
struct sockaddr_in * ip = reinterpret_cast<struct sockaddr_in *>(&(interface.ifr_addr));

unsigned long address = ntohl(ip->sin_addr.s_addr);
}

Share this post


Link to post
Share on other sites
See the FAQ #18 "How do I get the IP address of the machine I'm running on?"

Basically you should not need to get your LAN IP address.

However, I imagine that it's necessary to figure out what address to use for UDP broadcasts. Perhaps IP multicasts are preferred over broadcasts these days - I recommend reading the docs about multicasting.

Mark

Share this post


Link to post
Share on other sites
edit: <removed my flip remarks> ^_^' no need for it

I'll just parse netstat...

[Edited by - cppgirl on March 24, 2007 10:29:18 AM]

Share this post


Link to post
Share on other sites
You can use ioctl(socket,SIOCGIFCONF as described in some places in man pages,

also here:

http://groups.google.co.uk/group/comp.os.linux.development.apps/msg/efa770362cfac66f?hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=3

Mark

Share this post


Link to post
Share on other sites
Quote:
I imagine that it's necessary to figure out what address to use for UDP broadcasts.


You can broadcast to 255.255.255.255 and it'll reach everywhere in your local subnet, but be filtered at the router level.

Multicast is not preferred (and generally doesn't work), except in some custom LAN situations (internal networks).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!