Jump to content
  • Advertisement
Sign in to follow this  
Shashwat

How to get the IP address of the machine?

This topic is 2995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
In short, yes. You've got me curious, though. Is there a reason that you're trying to side-step the host name, when the entire thing is only three lines of code?

In case you don't know, there can be more than one IP address associated with a single computer.

Share this post


Link to post
Share on other sites
Quote:
Is there a reason that you're trying to side-step the host name, when the entire thing is only three lines of code?


No! Just asking its necessity for knowledge.
No matter the code is of 3 lines only.
We should know why is it so?


I've created a Chat Messenger which creates a chat server on the loopback address. And the other clients connect to it.

Now! I'm creating a Game to connect to another game in the LAN.
I'm not bothered here about the internet.

Still Would there be more than one IP addresses?

Share this post


Link to post
Share on other sites
Quote:
Original post by Shashwat
Still Would there be more than one IP addresses?


I couldn't possibly know. The number of IP addresses depends on the computer the software is running on; how it's connected to the network, if you've created virtual loopback adapters, if you're running virtual machines that share your network connection, etc.

If you have a virgin install of the OS and you only have one RJ45 cable, from the computer to the modem/hub/router/switch, then you should only have one IP address. You can't, however, rely on that to be true, or stay constant.

From the site that you linked to
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;


addr is an array of the current IP addresses associated with the computer returned by the DNS.GetHostByName(strHostName) function. It would, at that point, be up to you to figure out which one you needed.

Share this post


Link to post
Share on other sites
Quote:
Original post by AverageMidget
From the site that you linked to
*** Source Snippet Removed ***

addr is an array of the current IP addresses associated with the computer returned by the DNS.GetHostByName(strHostName) function. It would, at that point, be up to you to figure out which one you needed.
However - this will only give you the local IP of the machine if it's connected to a router (E.g. 192.168.0.1). If you want the external IP address, to allow other users on the Internet to connect to your machine, you'll need to use a third party to do that for you - for instance going to www.whatismyip.com and reading the IP address from the page, or if you have a web server, set up a script to return the IP address.

You cannot get the external IP address from the local machine without connecting to another server on the Internet.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
However - this will only give you the local IP of the machine if it's connected to a router (E.g. 192.168.0.1).


I was hoping that would be inferred, based on the fact that Shashwat wanted local IPs, but I probably should've been more explicit. Definitely worth noting, though.

Quote:
Original post by Evil Steve
...for instance going to www.whatismyip.com and reading the IP address from the page...


They have a special page, just for this. You don't have to do any screen-scraping, because it only returns your IP.

Share this post


Link to post
Share on other sites
Quote:
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;


I've already tried that basic code to retreive IP Address using hostname
The addresslist 'addr' contains just one IP Address ie 192.168.1.2 (in my case)

Quote:
If you want the external IP address, to allow other users on the Internet to connect to your machine, you'll need to use a third party to do that for you


Please tell me why is it so?
Why is there such mechanism to access the third party to get my internet IP Address?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!