Local IP address (SDL_net)

Hello all,

Does SDL_net provide a way of getting the local host’s IP addresss? Not the
loop-back address, but its routable address?

I’ve searched through SDL_net’s docs and demos, but I can’t seem to find this.
I tried the “dnr” demo for SDL_net, but it says that it cannot resolve the
hostname?

I’m using openSUSE 10.2 and the SDL libs:
SDL-1.2.11-22
SDL_net-1.2.6-2.1

I can do it using the Linux system calls, but I prefer to use SDL_net so the
solution will be automagically portable.

Thanks,

Alvin

Does SDL_net provide a way of getting the local host’s IP addresss? Not the
loop-back address, but its routable address?

No. BTW: you can have more than one such address!

I’ve searched through SDL_net’s docs and demos, but I can’t seem to find
this. I tried the “dnr” demo for SDL_net, but it says that it cannot
resolve the hostname?

Don’t know about that demo.

I can do it using the Linux system calls, but I prefer to use SDL_net so
the solution will be automagically portable.

Well, using the Berkeley sockets API (which is what you mean with system
calls), you are already pretty portable. Other than that, a patch to SDL_net
would surely also be welcome.

UliOn Wednesday 23 May 2007 18:12:41 Alvin wrote:

Yes you can…

Ulrich Eckhardt <doomster knuut.de> writes:

Does SDL_net provide a way of getting the local host’s IP addresss? Not the
loop-back address, but its routable address?

No. BTW: you can have more than one such address!

I’ve searched through SDL_net’s docs and demos, but I can’t seem to find
this. I tried the “dnr” demo for SDL_net, but it says that it cannot
resolve the hostname?

Don’t know about that demo.

I can do it using the Linux system calls, but I prefer to use SDL_net so
the solution will be automagically portable.

Well, using the Berkeley sockets API (which is what you mean with system
calls), you are already pretty portable. Other than that, a patch to SDL_net
would surely also be welcome.

Uli
// Check what my real IP is first
BUG("Check what my real IP is: ");
const char* tempstr = SDLNet_ResolveIP(&myIPaddress); // Get
the network name of this computer
if (tempstr) BUG(“tempstr is valid”);
if (strlen(tempstr) > 79) ERROR(“ERR: Filename too long!”);
strcpy(myNetworkName, tempstr);
fprintf(stdout,"myNetworkName is: “);
fprintf(stdout, " %s \n”, myNetworkName); fflush(stdout);

SDLNet_ResolveHost(&myIPaddress, myNetworkName, FOUNDATION_PORT);
// Fill up an ip with my network name
iterator = (Uint8*) &myIPaddress.host;			// This simple 

elegant code reads the host Uint32 member into a readable IP
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, "%d ", *iterator); //
fprintf(stdout,"myIPaddress.port is: “);
fprintf(stdout, " %d \n”, myIPaddress.port);
fflush(stdout);> On Wednesday 23 May 2007 18:12:41 Alvin wrote:

Ok, so where do you fill in the value of myIPaddress? =)On 10/06/07, Christopher Larsen wrote:

Yes you can…
// Check what my real IP is first
BUG("Check what my real IP is: ");
const char* tempstr = SDLNet_ResolveIP(&myIPaddress); // Get
the network name of this computer
if (tempstr) BUG(“tempstr is valid”);
if (strlen(tempstr) > 79) ERROR(“ERR: Filename too long!”);
strcpy(myNetworkName, tempstr);
fprintf(stdout,"myNetworkName is: “);
fprintf(stdout, " %s \n”, myNetworkName); fflush(stdout);

    SDLNet_ResolveHost(&myIPaddress, myNetworkName, FOUNDATION_PORT);
    // Fill up an ip with my network name
    iterator = (Uint8*) &myIPaddress.host;                  // This simple

elegant code reads the host Uint32 member into a readable IP
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, "%d ", *iterator); //
fprintf(stdout,"myIPaddress.port is: “);
fprintf(stdout, " %d \n”, myIPaddress.port);
fflush(stdout);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ok, so where do you fill in the value of myIPaddress? =)

Oh, sorry, that part is trivial

myIPaddress.host = NULL;
myIPaddress.port = 69;	

The trick is to call SDLNet_ResolveIP() with host NULL to
get your network name, which you feed back into SDLNet_ResolveHost()
to convert that network name to an IP.
Warning: if you have multiple network adapters, you’re on a LAN or wireless
or both, I have no idea which IP you’ll get back.
What I did is have comp1 connect to comp2,
and comp2 tells comp1 what it sees
comp1’s IP as. ( GetPeerAddress() )

Cheers!On 10/06/07, Christopher Larsen <christopherleelarsen gmail.com> wrote:

Yes you can…
// Check what my real IP is first
BUG("Check what my real IP is: ");
const char* tempstr = SDLNet_ResolveIP(&myIPaddress); // Get
the network name of this computer
if (tempstr) BUG(“tempstr is valid”);
if (strlen(tempstr) > 79) ERROR(“ERR: Filename too long!”);
strcpy(myNetworkName, tempstr);
fprintf(stdout,"myNetworkName is: “);
fprintf(stdout, " %s \n”, myNetworkName); fflush(stdout);

    SDLNet_ResolveHost(&myIPaddress, myNetworkName, FOUNDATION_PORT);
    // Fill up an ip with my network name
    iterator = (Uint8*) &myIPaddress.host;                  // This simple

elegant code reads the host Uint32 member into a readable IP
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, “%d.”, *iterator++); //
fprintf(stdout, "%d ", *iterator); //
fprintf(stdout,"myIPaddress.port is: “);
fprintf(stdout, " %d \n”, myIPaddress.port);
fflush(stdout);

Nope, that doesn’t seem to work for me. Does the machine you are using
this on have a valid DNS hostname or something? I changed
myAddress.host = 0; as NULL isn’t really a Uint32 (and so GCC emits a
warning).On 15/06/07, Christopher Larsen wrote:

Ok, so where do you fill in the value of myIPaddress? =)

Oh, sorry, that part is trivial

    myIPaddress.host = NULL;
    myIPaddress.port = 69;

The trick is to call SDLNet_ResolveIP() with host NULL to
get your network name, which you feed back into SDLNet_ResolveHost()
to convert that network name to an IP.
Warning: if you have multiple network adapters, you’re on a LAN or wireless
or both, I have no idea which IP you’ll get back.
What I did is have comp1 connect to comp2,
and comp2 tells comp1 what it sees
comp1’s IP as. ( GetPeerAddress() )

Cheers!