Advertisement

gethostbyaddr question?

Started by June 22, 2006 06:41 AM
6 comments, last by hplus0603 18 years, 8 months ago
I'm trying to resolve the IP address of a machine over LAN network I'm using ENet I do a call to enet_address_get_host() which in turn calls the function gethostbyaddr() the function call succeeds but instead of returning the ip address in the form "x.x.x.x" it returns "PC15.MyCompanyName.Corp" Is that a normal thing? and how i can get it in the form "x.x.x.x" thanks in advance
Storms .. Wind and Fire .. READ MY CALLS
gethostbyaddr() returns one of the hosts corresponding to an inet_addr. PC15.MyCompanyName.Corp is a host while x.x.x.x is not.

However, x.x.x.x would be the string representation of an IPv4 inet_addr, as returned by inet_ntoa.
Advertisement
If you really want to resolve the address of a host (name), like you say, you probably want getaddrbyname() -- perhaps there's an ENet wrapper function (my guess would be something like enet_host_get_address()).

The function you're calling takes an already existing IP address, and turns that into a textual name of some sort.

If you already have the address, but want the x.y.z.w textual form, you can easily create that with sprintf() in a portable way, as inet_ntoa() only exists on Windows.
enum Bool { True, False, FileNotFound };
You all right, but the actual problem is that i don't have the ip address number ( so i can operate inet_ntoa on it or convert it by myself ).

any way i've managed to get it the ip number - without calling any function out of ENet - and i am posting the code so others can make use of it:
unsigned int GetIPNumber(){	ENetAddress	address;	address.host = ENET_HOST_ANY;    //ENET_HOST_ANY is equivlant to INADDR_ANY	address.port = 1234;	char buffer[128];	//enet_address_get_host() calls -> gethostbyaddr()	//address[in], buffer[out]	enet_address_get_host( &address, buffer, 128 );	//buffer now contains "PC15.MyCompanyName.Corp".	//enet_address_set_host() calls -> gethostbyname()	//address[out], buffer[in]	enet_address_set_host( &address, buffer );	//address.host field now contains the ip address which is the number that i was looking for	return address.host;}

Thanks
Storms .. Wind and Fire .. READ MY CALLS
What your code does is look up the local IP address of the local host. That's not terribly useful to the rest of the world, if your host is behind a NAT firewall. Check the Forum FAQ for the section on how to find your IP, and why that's a bad idea.
enum Bool { True, False, FileNotFound };
Quote:
Original post by hplus0603
What your code does is look up the local IP address of the local host.


but that's what i wanted in first place =) sorry if i couldn't express my problem the right way...

i don't know what is NAT firewall! should i care for if my game will only run on a LAN?
Storms .. Wind and Fire .. READ MY CALLS
Advertisement
If your game will only run on a LAN, you don't need to worry about NAT -- but then, you also don't need to worry about your local IP, because you can do server discovery using broadcast UDP.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement