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.

June 28, 2015 | 4:20 pm

Using [mxj net.local] it is still not at all clear to me what all of the abbreviations that populate the umenu mean. I’m looking for a unique moniker to use as an (at least relatively) unchanging identifier of the computer my patch is running on. If there was a way to get the actual name of the computer that would be ideal, but I thought I might be able to use the public IP address instead. However the only abbreviation that returns an actual IP address, besides lo, is net6, and it is not my public IP (according to sites like mypublicip.com). A lot of the abbreviations return gibberish like

fe80:0:0:0:30da:d7e7:d9b0:387c%15

What does that stuff mean? How can I actually get my public IP address automatically using Max?


Jan
June 29, 2015 | 6:02 am

Hi TO_SE_SUN,
what you get seems to be a corrupted version (wrongly parsed) of your IP address in the format IPv6. If you want to get the public IP address this will do (plain max :) ):

— Pasted Max Patch, click to expand. —

As a unique identifier it has limitations as all machines within the same network, using the same internet gateway (router) will have the same IP.
More reliable would be to query your MAC address of the ethernet or wifi card. At least in theory the MAC address should be unique for each interface. But afaik there is no Java class for that that ships with Max. You wound need to write your own. Here’s a starting point:

http://www.mkyong.com/java/how-to-get-mac-address-in-java/

Cheers,
jan


Jan
June 29, 2015 | 6:13 am

Here’s a quick and dirty java class that reports the MAC address of your network interface. It’s jusr a 5 min speed coding, but works here on OS X. Just send bang into the the first inlet and the left outlet will report the MAC address.

Best place in Max7 is to put the .class and .java files in a java folder of a custom package – then restart Max.

J

Attachments:
  1. Archive.zip

Jan
June 29, 2015 | 6:18 am

here’s a slightly improved version: left outlet: MAC address, right outlet your IP in the local network. That should be enough information to uniquely identify every computer on the planet ;)

Attachments:
  1. macaddress_2.zip
Viewing 13 posts - 1 through 13 (of 13 total)

Forums > Java