繁体   English   中英

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

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

我正在尝试获取具有特定 LAN IP 地址的机器名称。

到目前为止我尝试的代码:

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

但它总是返回我输入的相同 IP。

如何解决这个问题?

检查您是否能够使用此 ip 192.168.1.101从终端或 cmd ping 该系统:

键入ping 192.168.1.101并按回车键或回车键

然后试试这段代码:

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());
    }

}

注意:ip - 192.168.1.101的系统应该存在,否则会报错。

暂无
暂无

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

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