简体   繁体   English

如何使用 android 中的 IP 获取 LAN 上系统的主机名?

[英]How to get the host name of a system on the LAN using IP in android?

I'm trying to get the machine name with specific LAN IP address.我正在尝试获取具有特定 LAN IP 地址的机器名称。

The code I tried so far:到目前为止我尝试的代码:

InetAddress addresss = InetAddress.GetByName("192.168.1.101");
bool test = addresss.IsReachable(500);
string name = addresss.CanonicalHostName;
string name0 = addresss.HostName;

But it always return the same IP that I have entered.但它总是返回我输入的相同 IP。

How to resolve this?如何解决这个问题?

Check whether are you able to ping that system with this ip 192.168.1.101 from terminal or cmd:检查您是否能够使用此 ip 192.168.1.101从终端或 cmd ping 该系统:

Type ping 192.168.1.101 and press return or Enter键入ping 192.168.1.101并按回车键或回车键

Then try this code:然后试试这段代码:

public class Example {

    public static void main(String[] args) throws UnknownHostException {
        InetAddress address = InetAddress.getByName("192.168.1.101");
        System.out.println("Machine Name : " + address.getHostName());
    }

}

Note: The system with ip - 192.168.1.101 should exist, otherwise you will get error.注意:ip - 192.168.1.101的系统应该存在,否则会报错。

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

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