I am trying to run an application Pharo that requires data from the Ip of the host computer in which it's running. In general I have to open the code to set it manually in my Pharo code then launch the application. Is there a better and programmatically way of getting the IP address and returning this address automatically regardless of the computer? Say I deploy it to different computer.
-
please don't forget to mark your questions answered when you are happy with an answer! For more see https://stackoverflow.com/help/someone-answers – tukan Jul 12 '18 at 13:40
-
There is no such thing as "the ip address". Computers can have multiple network connections (wifi, wired) that each have their own address. – Stephan Eggermont Aug 20 '18 at 10:48
1 Answers
I think you are searching for NetResolverName
.
To get name of your computer:
NetNameResolver localHostName
Then you can use it for the IP address. If you execute in Workspace:
NetNameResolver addressForName: NetNameResolver localHostName
You will get a local network address.
In my case: #[192 168 1 17]
Edit
You can also directly query it via:
NetNameResolver localAddressString
Where you will get just: '192 168 1 17'
Note: If you are getting 127.0.0.1
If you are getting localhost
address => 127.0.0.1
you probably have issues with the host mapping.
You can test that with via ping "hostname"
e.g. ping localhost.localdomain
which will get you replies from 127.0.0.1
. If you have correct setup (hosts, DNS, etc.) you should get reply from your IP address.
When I have changed the hostname
from localhost.localdomain
to smalltalk
then ping smalltalk correctly replies with 192.168.10.30
. Then Pharo
produces correct result.

- 17,050
- 1
- 20
- 48
-
doing so I get the loopback address 127.0.0.1. Instead I need the private Ip like in your case. – ludo Jul 12 '18 at 11:45
-
-
-
-
-
@ludo if you do `ping mylinux` what IP address is queried? (My guess is that you don't have the name to IP mapping done correctly) The example above was run on windows it correctly returns the IP when pinged. When I tried on my linux VM with `ping localhost.localdomain` I got `127.0.0.1` return => Pharo has shown `127.0.0.1`. When I changed the name `sudo hostnamectl set-hostname smalltalk` (the `smalltalk` hostname name is mapped to the `192.168.10.30`) then `ping smalltalk` correctly returns `192.168.10.30` and so does Pharo. – tukan Jul 12 '18 at 12:45
-
you are actually right. ping will return 127.0.0.1. Does it mean I have to make change in the /etc/hosts? I changed the name it does not resolve to anything. – ludo Jul 12 '18 at 13:09
-
@ludo that depends on your network setup. The *easiest*, not always correct thou, is to make changes in your `/etc/hosts`. – tukan Jul 12 '18 at 13:21
-
It does work fine on windows. I will make some change and let you know. Thank you. – ludo Jul 12 '18 at 13:51
-
@ludo you should upvote this answer (in addition to checking it with the green mark). Thanks! – Leandro Caniglia Jul 12 '18 at 21:33