SDL_net: How to retrieve the IP Address of localhost, SDLNet_ResolveHost()?

Hi there,
After I figured out how to broadcast from Win7, now have got another problem. The broadcast works on W7 when I set the host with 3 bytes matching my local network (e.g. 192.168.1.x) where x is to be set to 255. That means I have to retrieve the 3 bytes of the localhost. When I used SDLNet_ResolveHost() I got the values 127.0.0.1. How can I retrieve the IP address of the localhost?

Thanks
Phuoc

(I’m running linux, so I can’t test this, but give it a try):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

void printIP( IPAddress *ip ) {
Uint8 parts[4] = (Uint8 *)ip->host;
printf( “%i.%i.%i.%i”, (int)parts[3], (int)parts[2], (int)parts[1],
(int)parts[0] );
}

int main( int argc, char *argv ) {
// Blah blah blah init SDL and SDL_net() and whatever

int port = 12345;

char *computer = getComputerName();
IPAddress ip;
SDLNet_ResolveHost( &ip, computer, port );
free( computer );
printIP( &ip );

// Blah blah blah
}

I hope this helps,
-AlexOn Mon, Nov 29, 2010 at 6:35 AM, Phuoc Can Hua wrote:

Hi there,
After I figured out how to broadcast from Win7, now have got another
problem. The broadcast works on W7 when I set the host with 3 bytes matching
my local network (e.g. 192.168.1.x) where x is to be set to 255. That means
I have to retrieve the 3 bytes of the localhost. When I used
SDLNet_ResolveHost() I got the values 127.0.0.1. How can I retrieve the IP
address of the localhost?

Thanks
Phuoc


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

Assuming this is not time-critical, wouldn’t it be possible to run
ifconfig/ipconfig using a system() call and parse the output from the
command?On 11/29/10 8:33 AM, Alex Barry wrote:

(I’m running linux, so I can’t test this, but give it a try):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

void printIP( IPAddress *ip ) {
Uint8 parts[4] = (Uint8 *)ip->host;
printf( “%i.%i.%i.%i”, (int)parts[3], (int)parts[2], (int)parts[1],
(int)parts[0] );
}

int main( int argc, char *argv ) {
// Blah blah blah init SDL and SDL_net() and whatever

int port = 12345;

char *computer = getComputerName();
IPAddress ip;
SDLNet_ResolveHost( &ip, computer, port );
free( computer );
printIP( &ip );

// Blah blah blah
}

I hope this helps,
-Alex

On Mon, Nov 29, 2010 at 6:35 AM, Phuoc Can Hua <phuoccanhua at yahoo.com.au <mailto:phuoccanhua at yahoo.com.au>> wrote:

Hi there,
After I figured out how to broadcast from Win7, now have got
another problem. The broadcast works on W7 when I set the host
with 3 bytes matching my local network (e.g. 192.168.1.x) where x
is to be set to 255. That means I have to retrieve the 3 bytes of
the localhost. When I used SDLNet_ResolveHost() I got the values
127.0.0.1. How can I retrieve the IP address of the localhost?

Thanks
Phuoc



_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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

He’s using windows 7, not *nixOn Mon, Nov 29, 2010 at 11:56 AM, Andreas Schiffler <aschiffler at ferzkopp.net wrote:

Assuming this is not time-critical, wouldn’t it be possible to run
ifconfig/ipconfig using a system() call and parse the output from the
command?

On 11/29/10 8:33 AM, Alex Barry wrote:

(I’m running linux, so I can’t test this, but give it a try):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

void printIP( IPAddress *ip ) {
Uint8 parts[4] = (Uint8 *)ip->host;
printf( “%i.%i.%i.%i”, (int)parts[3], (int)parts[2], (int)parts[1],
(int)parts[0] );
}

int main( int argc, char *argv ) {
// Blah blah blah init SDL and SDL_net() and whatever

int port = 12345;

char *computer = getComputerName();
IPAddress ip;
SDLNet_ResolveHost( &ip, computer, port );
free( computer );
printIP( &ip );

// Blah blah blah
}

I hope this helps,
-Alex

On Mon, Nov 29, 2010 at 6:35 AM, Phuoc Can Hua wrote:

Hi there,
After I figured out how to broadcast from Win7, now have got another
problem. The broadcast works on W7 when I set the host with 3 bytes matching
my local network (e.g. 192.168.1.x) where x is to be set to 255. That means
I have to retrieve the 3 bytes of the localhost. When I used
SDLNet_ResolveHost() I got the values 127.0.0.1. How can I retrieve the IP
address of the localhost?

Thanks
Phuoc


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


SDL mailing listSDL at lists.libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


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

Oh, my brain missed ipconfig. Regardless, checking for the computer name is
probably the more reliable method, and there is probably a win32 api/*nic
call to get the computer name without going through all the hoops I did.On Mon, Nov 29, 2010 at 11:58 AM, Alex Barry <@Alex_Barry> wrote:

He’s using windows 7, not *nix

On Mon, Nov 29, 2010 at 11:56 AM, Andreas Schiffler < aschiffler at ferzkopp.net> wrote:

Assuming this is not time-critical, wouldn’t it be possible to run
ifconfig/ipconfig using a system() call and parse the output from the
command?

On 11/29/10 8:33 AM, Alex Barry wrote:

(I’m running linux, so I can’t test this, but give it a try):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

void printIP( IPAddress *ip ) {
Uint8 parts[4] = (Uint8 *)ip->host;
printf( “%i.%i.%i.%i”, (int)parts[3], (int)parts[2], (int)parts[1],
(int)parts[0] );
}

int main( int argc, char *argv ) {
// Blah blah blah init SDL and SDL_net() and whatever

int port = 12345;

char *computer = getComputerName();
IPAddress ip;
SDLNet_ResolveHost( &ip, computer, port );
free( computer );
printIP( &ip );

// Blah blah blah
}

I hope this helps,
-Alex

On Mon, Nov 29, 2010 at 6:35 AM, Phuoc Can Hua wrote:

Hi there,
After I figured out how to broadcast from Win7, now have got another
problem. The broadcast works on W7 when I set the host with 3 bytes matching
my local network (e.g. 192.168.1.x) where x is to be set to 255. That means
I have to retrieve the 3 bytes of the localhost. When I used
SDLNet_ResolveHost() I got the values 127.0.0.1. How can I retrieve the IP
address of the localhost?

Thanks
Phuoc


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


SDL mailing listSDL at lists.libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


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

Unless I’m missing something, it looks like you reimplemented getenv(
“COMPUTERNAME” )?On 29/11/2010 16:33, Alex Barry wrote:

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

Nope, it was me who missed it. Good call, Tim.
Also, there is the windows api call GetComputerName() demonstrated here:
Getting System Information - Win32 apps | Microsoft Learn**
On Mon, Nov 29, 2010 at 12:06 PM, Tim Angus wrote:

On 29/11/2010 16:33, Alex Barry wrote:

char *getComputerName() {
system( “echo %COMPUTERNAME% > cname.txt” );
FILE *handle = fopen( “cname.txt”, “r” );
char cname[33];
fread( cname, 1, 32, handle );
fclose( handle );
int len = strlen( cname ) + 1;
char *ret = (char *)alloc( 1, sizeof( char ) * len );
memset( ret, ‘\0’, len );
memcpy( cname, ret, len-1 );
return ret;
}

Unless I’m missing something, it looks like you reimplemented getenv(
“COMPUTERNAME” )?


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

Unless you have a good reason not to, you should just use
255.255.255.255 instead of trying to figure out a specific broadcast
address.On 29/11/2010 11:35, Phuoc Can Hua wrote:

Hi there, After I figured out how to broadcast from Win7, now have
got another problem. The broadcast works on W7 when I set the host
with 3 bytes matching my local network (e.g. 192.168.1.x) where x is
to be set to 255. That means I have to retrieve the 3 bytes of the
localhost. When I used SDLNet_ResolveHost() I got the values
127.0.0.1. How can I retrieve the IP address of the localhost?

Thank you for all the suggestions. I’ll try with GetComputerName() name first and see how it goes.

Thanks
Phuoc— On Tue, 30/11/10, Alex Barry <alex.barry at gmail.com> wrote:

From: alex.barry@gmail.com (Alex Barry)
Subject: Re: [SDL] SDL_net: How to retrieve the IP Address of localhost, SDLNet_ResolveHost()?
To: “SDL Development List”
Received: Tuesday, 30 November, 2010, 4:15 AM

Nope, it was me who missed it.? Good call, Tim.
Also, there is the windows api call GetComputerName() demonstrated here:
http://msdn.microsoft.com/en-us/library/ms724426(v=VS.85).aspx

Received: Tuesday, 30 November, 2010, 4:04 AM

Hi there, After I figured out how to broadcast from
Win7, now have
got another problem. The broadcast works on W7 when I
set the host
with 3 bytes matching my local network (e.g.
192.168.1.x) where x is
to be set to 255. That means I have to retrieve the 3
bytes of the
localhost. When I used SDLNet_ResolveHost() I got the
values
127.0.0.1. How can I retrieve the IP address of the
localhost?

Unless you have a good reason not to, you should just use
255.255.255.255 instead of trying to figure out a specific
broadcast address.

Unfortunately, it just doesn’t want to work on Win7, even when I turned off all firewalls :frowning: It was all fine with 255.255.255.255 on XP.— On Tue, 30/11/10, Tim Angus wrote:

From: Tim Angus
Subject: Re: SDL_net: How to retrieve the IP Address of localhost, SDLNet_ResolveHost()?
To: home at huaonline.com, “SDL Development List”
On 29/11/2010 11:35, Phuoc Can Hua wrote:

For Win 7 have you tried running the app with Administrator Privledges?> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Phuoc Can Hua
Sent: Monday, November 29, 2010 3:54 PM
To: SDL Development List
Subject: Re: [SDL] SDL_net: How to retrieve the IP Address of localhost,
SDLNet_ResolveHost()?

— On Tue, 30/11/10, Tim Angus wrote:

From: Tim Angus
Subject: Re: SDL_net: How to retrieve the IP Address of localhost,
SDLNet_ResolveHost()?
To: home at huaonline.com, “SDL Development List”
Received: Tuesday, 30 November, 2010, 4:04 AM
On 29/11/2010 11:35, Phuoc Can Hua wrote:

Hi there, After I figured out how to broadcast from
Win7, now have
got another problem. The broadcast works on W7 when I
set the host
with 3 bytes matching my local network (e.g.
192.168.1.x) where x is
to be set to 255. That means I have to retrieve the 3
bytes of the
localhost. When I used SDLNet_ResolveHost() I got the
values
127.0.0.1. How can I retrieve the IP address of the
localhost?

Unless you have a good reason not to, you should just use
255.255.255.255 instead of trying to figure out a specific
broadcast address.

Unfortunately, it just doesn’t want to work on Win7, even when I turned off
all firewalls :frowning: It was all fine with 255.255.255.255 on XP.


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

Received: Tuesday, 30 November, 2010, 8:56 AM
For Win 7 have you tried running the
app with Administrator Privledges?
I did. I used “Run as admin” from context menu.— On Tue, 30/11/10, Ken Rogoway wrote:
From: Ken Rogoway
Subject: RE: [SDL] SDL_net: How to retrieve the IP Address of localhost, SDLNet_ResolveHost()?
To: home at huaonline.com, “‘SDL Development List’”

I tried broadcasting to 255.255.255.255 on localhost and it worked. Is
Windows 7 the host or the client? I might be able to test your setup
here…>> Unless you have a good reason not to, you should just use

255.255.255.255 instead of trying to figure out a specific
broadcast address.

Unfortunately, it just doesn’t want to work on Win7, even when I turned off all firewalls ?:frowning: It was all fine with 255.255.255.255 on XP.

Received: Tuesday, 30 November, 2010, 10:55 AM
I tried broadcasting to
255.255.255.255 on localhost and it worked. Is
Windows 7 the host or the client? I might be able to test
your setup
here…

It works from XP to W7.
My main program broadcast info and normally I run a “client” on the localhost. This is working fine even on W7. But when I start another client on an XP machine it doesn’t receive anything.
(as mentioned it works fine with 255.255.255.255. to broadcast from XP)— On Tue, 30/11/10, Brian Barrett <brian.ripoff at gmail.com> wrote:

From: Brian Barrett <brian.ripoff at gmail.com>
Subject: Re: [SDL] SDL_net: How to retrieve the IP Address of localhost, SDLNet_ResolveHost()?
To: home at huaonline.com, “SDL Development List”