🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

IP Address of local machine?

Started by
4 comments, last by POW 24 years, 3 months ago
In some software I am writing I need to determine the IP address of the local computer (like using ipconfig). Would some one be able to tell me the simplest method of programming this? Thanx POW
Advertisement
well, the localhost ip is normally 127.0.0.1, but I doubt that helps you...what API are you using? Berkley Sockets? DirectPlay?

"When people tell you they want to hear the truth, you know that their lying."
I need the IP address of the local computer which will be acting as a server. Once I have the local IP I will call up a client computer using dialogic cards (telephony cards) and tell that client what IP address to try to connect to using WinSock.
This may seem confusing but there is a purpose.

Thanx POW
127.0.0.1 is the ip address of the loopback interface which is usually bound to the name "localhost". However, if you what you need to know the IP address of other interfaces you can use the following code from Shadow, posted on the board at the end of February in the thread "What function returns the IP address of host machi"

quote:
char localHost[256];
gethostname(localHost,256);

then use it like

GetIpFrom(localhost);

and it returns a char* with the ip (then you can convert the ip to any other format you want)

char *GetIpFrom(char *hostname)
{
hostent *host;
struct in_addr local;
long hs;
static index = 0;

host = gethostbyname(hostname);

if(!host->h_addr_list[index]) return "None";
memcpy(&hs,host->h_addr_list[index],4);

local.S_un.S_addr = hs;

index++;

return inet_ntoa(local);
}
Note: isn''t all 127.*.*.* IP''s loopbacks?

============================
Daniel Netz, Sentinel Design
"I'm not stupid, I'm from Sweden" - Unknown
============================Daniel Netz, Sentinel Design"I'm not stupid, I'm from Sweden" - Unknown
The IP specification states that any address in the subnet 127/8 can be assigned to the loopback interface. Some implementations assign all the 127/8 ip''s to loopback. Some only assign 127.0.0.1. And usually it''s 127.0.0.1 thats returned when you perform a name lookup on "localhost". Also 127.255.255.255 and 127.0.0.0 are not valid loopback addresses (for obvious reasons).

This topic is closed to new replies.

Advertisement