简体   繁体   English

是否有一种特定的方法来获取Pharo smalltalk中的计算机的IP?

[英]Is there a specific way to get the Ip of a computer in Pharo smalltalk?

I am trying to run an application Pharo that requires data from the Ip of the host computer in which it's running. 我正在尝试运行一个应用程序Pharo,它需要来自运行它的主机的IP的数据。 In general I have to open the code to set it manually in my Pharo code then launch the application. 一般情况下,我必须打开代码,在我的Pharo代码中手动设置它,然后启动应用程序。 Is there a better and programmatically way of getting the IP address and returning this address automatically regardless of the computer? 是否有更好的编程方式获取IP地址并自动返回此地址而不管计算机是什么? Say I deploy it to different computer. 假设我将其部署到不同的计算机上。

I think you are searching for NetResolverName . 我想你正在搜索NetResolverName

To get name of your computer: 要获取计算机的名称:

NetNameResolver localHostName

Then you can use it for the IP address. 然后,您可以将其用于IP地址。 If you execute in Workspace: 如果您在Workspace中执行:

NetNameResolver addressForName: NetNameResolver localHostName

You will get a local network address. 您将获得本地网络地址。

In my case: #[192 168 1 17] 就我而言: #[192 168 1 17]

Edit 编辑

You can also directly query it via: 您也可以通过以下方式直接查询:

NetNameResolver localAddressString

Where you will get just: '192 168 1 17' 你会得到的地方: '192 168 1 17'

Note: If you are getting 127.0.0.1 注意:如果您获得127.0.0.1

If you are getting localhost address => 127.0.0.1 you probably have issues with the host mapping. 如果您获得localhost address => 127.0.0.1 ,则可能存在主机映射问题。
You can test that with via ping "hostname" eg ping localhost.localdomain which will get you replies from 127.0.0.1 . 您可以通过ping "hostname"进行测试,例如ping localhost.localdomain ,它将从127.0.0.1获得回复。 If you have correct setup (hosts, DNS, etc.) you should get reply from your IP address. 如果您有正确的设置(主机,DNS等),您应该从您的IP地址获得回复。

When I have changed the hostname from localhost.localdomain to smalltalk then ping smalltalk correctly replies with 192.168.10.30 . 当我将hostnamelocalhost.localdomain更改为smalltalk ping smalltalk正确回复192.168.10.30 Then Pharo produces correct result. 然后Pharo产生正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM