Forums > Java

Getting IP Address of localhost

August 9, 2012 | 11:27 am

(*** This is not a question, but rather a feature request.)

Using [mxj net.ip], one can get the IP address of the localhost, that is 127.0.0.1,
but this is apparently NOT the IP address of the current machine.
If [mxj net.ip] supports such feature, it would be very useful.

FYI:
For my own project, I created a very simple Java external to get the IP address of the current machine with this simple code:
outlet(0, InetAddress.getLocalHost().getHostAddress());


August 9, 2012 | 10:14 pm

Use "mxj net.local" instead.


August 9, 2012 | 11:27 pm

[mxj net.local] builds up the list of the available network interfaces,
and selecting one of them returns an IP address, but none of them is correct.

Can you confirm that it works as expected on your machine?

(Maybe a problem in Mac OS X 10.8 Mountain Lion.)


August 10, 2012 | 12:20 am

I haven’t tested all interfaces in net.local but when looking at this again I think you’re approaching this in the wrong way.

When looking at net.ip (which you used first) you make some false assumptions it seems. You fed it ‘localhost’ (which is basically a computer name) which the net.ip routine then resolved into 127.0.0.1. You say its not the IP address of the current computer, but that is incorrect. When networked computers have a minimum of 2 IP addresses assigned to them; one for the localhost interface and one for the regular network interface.

However… It seems you’re overlooking something here. All net.ip does is resolve hostnames. Feed it a hostname and it looks up the associated IP address. So if you want to know the (public) IP address of your computer just feed it the (public) hostname. I named my (Windows) machine ‘plwin7′, and when I feed that into net.ip I get my lan IP address.

I think that could be much easier on you.


August 10, 2012 | 1:35 am

Ah, OK.
On my Mac, I put the name of the machine, and it returns the right IP address.
I did not realise this would work.

But still this is not really perfect,
because my patch then should get the name of the current machine.
(Do you have any clue?)

To be clear:
This is about the IP address in a local network.
For example, my Mac is attached to a router and the IP address is 192.128.0.11.
And what I want to do is getting this IP address automatically in Max.
But both [mxj net.ip] and [mxj net.local] have no way to get this IP address (unless the name of the machine is given).


August 10, 2012 | 2:03 am

no problem here, did you try to connect a print object to the left output of mxj net.local? the helppatch design with its umenu-display is suboptimal, did you do the things in right order:
1. bang
2. select umenu active networking connection (probably "en1")
3. check umenu "ip-adresses" (click on arrows, what you want might be hidden)


August 10, 2012 | 2:37 am

Oops, you are right, tobiasros.
The IP address I want to get is hidden there.

One thing I’m still concerned is that I need to choose the right network interface.
It shows four interfaces on my Mac, and the one I need is "en0", but this will be surely different on other machines.

The easiest solution for me is to use my own Java external with this simple code (as in my first posting):
> InetAddress.getLocalHost().getHostAddress() <

But now I begin to wonder if this approach may not be reliable in certain occasions, for example there may be more than one network connections at the same time (ethernet and wi-fi), and I need to choose one of them somehow.

Anyway, thanks a lot for your help, ShelLuser and tobiasros!!!


August 10, 2012 | 3:04 am

Well, not knowing what network device to use is basically a standard issue with tcp/ip based networking. And its solved through the so called ‘routing table’. This routing table is something you normally have no control over, but it tells the computer how it should reach the outside world. And to make sure you don’t have to specify every goal on the planet there’s also something as the ‘default route’. This basically tells your computer "if you can’t reach the goal directly, use this interface (or route, or…).

So; I don’t know what your exact goal is here but I do wonder if it isn’t possible to skip the whole identification step and just go along to send out whatever you’re after? Basically let the computer sort it all out.


August 10, 2012 | 3:18 pm

dont forget that your computer could be part of more than one network. there is not only local adresses and your IP for the intenet, therhe could also be a second ethernetcard for another network. this is why it is so surprisingly not straight forward to find the internet IP adress or the current/default interface using java.
one way how to find your own IP easily is when the application is a service and there is a client connected via in the internet. asking the connected client is an often used way for a server application ti find/know/show its one adress.


Viewing 9 posts - 1 through 9 (of 9 total)