[SDL] SDLnet: get my own IP

Daniel Stiefelmaier mail at stiefelweb.de
Wed Mar 20 15:06:01 PST 2002


Hi!

Maybe I did not find the function that I'm looking for, but I don't think
so...

I want to get my local IP as a String...

I did the following:

 char myIP[50];
 char *localname;
 IPaddress localIP;

     // get localhost-IP (127.0.0.1) in localIP
 SDLNet_ResolveHost(&localIP, "localhost", 0);
     // get name of localhost
 localname = SDLNet_ResolveIP(&localIP);
     // get real IP of local host
 SDLNet_ResolveHost(&localIP, localname, 0);
 IP2String(&localIP, myIP);

Code of
    IP2String(&localIP, myIP)
is at the end of text.
I'd like to have such one included in SDLnet!

I expected to find my IP in
char * myIP
at the end.
Using ping on the console (win98) worked
ping localhost
answer    "myhostname" and 127.0.0.1
ping "hostname"
answer   "myhostname" and 192.168.7.1

But in my code i get "NULL" in localname, maybe this is because i have a
computer name, but not a domain?

Why am I doing all this?
Well, i want to show the his ip to player with the server, so that he can
tell his "opponent" where he has to connect to.
But showing him 127.0.0.1 is a little bit senseless... ;-)

Thanx in Advance
Stiefel


BTW:
I really don't understand the advantage of swapped bytes or nibbles...
Is the following function correct for all systems?

void IP2String(IPaddress *ipaddress, char *ipString)
{
 Uint32 IP = ipaddress->host;
 int hnibble, lnibble;
 char part[5];

 lnibble = IP % 16;
 IP/=16;
 hnibble = IP % 16;
 IP/=16;
 itoa(hnibble * 16 + lnibble, ipString, 10);
 strcat(ipString,".");

 lnibble = IP % 16;
 IP/=16;
 hnibble = IP % 16;
 IP/=16;
 itoa(hnibble * 16 + lnibble, part, 10);
 strcat(ipString,part);
 strcat(ipString,".");

 lnibble = IP % 16;
 IP/=16;
 hnibble = IP % 16;
 IP/=16;
 itoa(hnibble * 16 + lnibble, part, 10);
 strcat(ipString,part);
 strcat(ipString,".");

  lnibble = IP % 16;
 IP/=16;
 hnibble = IP % 16;
 IP/=16;
 itoa(hnibble * 16 + lnibble, part, 10);
 strcat(ipString,part);
}






More information about the SDL mailing list