Sign in to follow this  

How to get the IP address of the machine?

This topic is 2651 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

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
I'm using Windows XP.

I've set my IP address as 'Obtain Automatically'.
Does it means that while connecting to the Internet, the DNS assigns the IP Address automatically to my PC?

And what would be my local IP Address in this case?
It showing 192.168.1.2. How it get that?
Please elaborate!

Share this post


Link to post
Share on other sites
Quote:
Original post by Shashwat
I'm using Windows XP.

I've set my IP address as 'Obtain Automatically'.
Does it means that while connecting to the Internet, the DNS assigns the IP Address automatically to my PC?

And what would be my local IP Address in this case?
It showing 192.168.1.2. How it get that?
Please elaborate!


Much like a home router, your ISP uses DHCP (Dynamic Host Configuration Protocol) to assign you an available IP.

192.168.1.2 was likely assign by a router.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shashwat
I've set my IP address as 'Obtain Automatically'.
Does it means that while connecting to the Internet, the DNS(1) assigns the IP Address automatically to my PC?
Your ISP uses DHCP to assign your *connection* an IP address. The reason I stress 'connection', is that that IP address is typically used by your router, since most people have a router between their computer and their modem.

Your computer will be assigned a local IP address by your router, typically in the 192.168.x.x range. This address can only be used to reach you by other machines on the same local netwrok (i.e. your router and anything plugged into it).

When you make an outgoing connection to somewhere beyond your local network (i.e. the internet), your router acts as a forward, and the outside server will only ever see the router's IP address - the router keeps track of which port it is forwarding your communication on, so that it can forward the responses back to you.

(1) DNS is used to assign hostnames. DHCP assigns IP addresses

Share this post


Link to post
Share on other sites
Ok Now I understand...

Please correct me now!

Local IP is the address of that port of router to which our PC is connected. So we can find it out.

Network IP address is the address of that port of router which is towards the rest of the internet and we can not directly access it.
External source (www.whatsmyip.com) can do so and that's why we have to use them to retrieve our IP address to connect to the internet.

Please correct me!

Share this post


Link to post
Share on other sites
Roughly, this is how it works:

Your ISP gives you an IP address which identifies your connection with the Internet. With this IP in hands, you can assign it directly to a computer, as in:
ISP (190.190.190.190) ---- Your Computer (190.190.190.190)

or you can put a router in the middle, as in
ISP (190.190.190.190) ---- Your router (190.190.190.190) ---- Your computer (???.???.???.???)

Given that an IP should be unique to external visitors (think about what would happen if two websites shared somehow the same IP, if you tried to access it, to which one would you actually connect?), your computer and router can't possibly have the same address.

This is where the distinction of "local" versus "global" address comes into play. See the IP addresses I've put on the "diagrams" above. The first IP, from the ISP, is the IP given to you when you subcribed for the Internet service. You can assign it to something. If it is "directly" (well, there must be at least a modem somewhere, but I'll ignore it) assigned to your computer, done. You have a "global" IP on your computer, congrats. But nowadays it's more than common to have a router in between. Since they can't have the same IP, "Your computer" is assigned, by the router, a local IP, that is, an IP that is only visible to the router and all the clients on the same network. Think about this situation now:
ISP (190.190.190.190) ---- router (190.190.190.190) ---- computer1 (192.168.0.100)
\---- computer2 (192.168.0.101)


You have two computers to which the router has assigned local addresses so they can talk to each other using this specific address. For instance, computer1 can connect directly to computer2 by connecting to 192.168.0.101. But someone that is not directly connected to the router can't. If a computer with IP 190.189.189.189 tries to connect to 192.168.0.101, it won't connect to computer2, because that address is local to the network, they aren't visible to the outside. As such, if those two computers need to talk, a different method for communication should be established. And this is done usually by the router itself, through the use of NAT.

NAT allows you to communicate from a "private address space" to a "public address space". (You might need to check this before moving on to get what a port really is) If computer2 makes a connection to 209.85.135.147:80, first a packet will be sent to the router, which will identify it as coming from the local address 192.168.0.101 at a certain port, for example 32323, and leaving to the outside world to the given IP and port. It will then store this information as a {[outgoing, 209.85.135.147, 80], [origin, 192.168.0.101, 32323]} pair and send the packet to the outside world.

If the server replies to you, the router will get a packet addressed to 190.190.190.190:32323. At this point, by looking at the previously stored information, the router knows that the connection coming to port 32323 is a reply to the one sent by the computer with local IP address 192.168.0.101. Once it knows where it originated, it will translate the destination in the packet to 192.168.0.101:32323 and send it to the local network. You got a connection back, for this text, end of story :)

This is a method that is used to allow you to have more that one computer running under the same IP address. It's not that only external sources can access you, it's more like you have to tell them from where you came before they can reach you. If, for example, some random computer tries to connect to 190.190.190.190:12345, the router will look at the table that contains all those {outgoing,origin} pairs to look for an outgoing connection on port 12345, but if it doesn't find it, the router won't really know what to do, since no computer "told where they came from".

Share this post


Link to post
Share on other sites
Thanks for writing so much!
But I know all this.
I know NAT.

I just want anyone to correct me

Quote:
Please correct me!

Local IP is the address of that port of router to which our PC is connected. So we can find it out.

Network IP address is the address of that port of router which is towards the rest of the internet and we can not directly access it.
External source (www.whatsmyip.com) can do so and that's why we have to use them to retrieve our IP address to connect to the internet.

Please correct me!


After an IP Address has been assigned to the Router, why do we need external source to find it out? I just gave an answer to this question.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shashwat
After an IP Address has been assigned to the Router, why do we need external source to find it out? I just gave an answer to this question.
You could just ask the router to tell you its external IP, since it must know its own IP. However, standard routers don't support any query to find this out.

Unless maybe you can abuse a traceroute-style operation, but that wouldn't work on a corporate-type network with pings filtered.

Also keep in mind that there are edge cases (i.e. in Mobile IP), where the route to an external host is *not* symmetrical. In this case you need to find out the reply-to address seen by the remote server, rather than your own subnet's outgoing address.

Share this post


Link to post
Share on other sites
Quote:
You could just ask the router to tell you its external IP, since it must know its own IP.


For this, I think we have to use the console cable to enter the configuration mode of the router. Such queries cannot be performed using Ethernet wire.
If you could have done that, then why would you needed www.whatsmyip.com??

Share this post


Link to post
Share on other sites
Quote:
Original post by Shashwat
For this, I think we have to use the console cable to enter the configuration mode of the router. Such queries cannot be performed using Ethernet wire.
If you could have done that, then why would you needed www.whatsmyip.com??


The point was that the router knows its external IP, but there's generally no way to find it out from the router:
Quote:
Original post by swiftcoder
You could just ask the router to tell you its external IP, since it must know its own IP. However, standard routers don't support any query to find this out.

Share this post


Link to post
Share on other sites

This topic is 2651 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this