【问题标题】:how to know ip address of a computer by using its pc name in ubuntu如何通过在ubuntu中使用计算机名称来知道计算机的IP地址
【发布时间】:2018-08-13 23:29:39
【问题描述】:

我目前正在开发一个聊天应用程序。 为了将客户端连接到服务器,我必须手动输入服务器的 IP 地址,因为我们使用的是 LAN,并且每次连接服务器系统时,它的 ip 都会更改。 那么有没有一种方法可以通过仅使用其电脑名称来查找特定计算机的 IP 地址。 我目前正在使用 Java 进行实现。

【问题讨论】:

标签: java ubuntu chat ip-address


【解决方案1】:
InetAddress host = InetAddress.getByName("www.java2novice.com");
System.out.println(host.getHostAddress());

此链接应该对您有所帮助。 http://www.java2novice.com/java_networking/ip_by_host_name/

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
  • 我不确定您所说的长期价值是什么意思。当您将 machineName/Hostname 传递给该方法时,它会毫无问题地为您获取 IP。只有当提供的机器名不正确时才会抛出 UnkownHostException
【解决方案2】:
InetAddress ip = InetAddress.getByName("Host_Name"); // just write the host name of server machine
    System.out.println(ip.getHostAddress());

我认为这会对你有所帮助。

【讨论】:

  • 把上面的代码放到try-catch块中或者抛出UnknownHost Exception。
【解决方案3】:

如果您想获取 Ubuntu PC 的唯一 IP 地址,请输入:

$ hostname -I

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2017-10-25
    • 2014-08-08
    • 1970-01-01
    • 2012-02-05
    • 2013-03-28
    相关资源
    最近更新 更多