我希望能制作一个小型的安卓应用程序,从连接的局域网中获取IP地址和主机名。我已经编写了一个代码,在连接的局域网中可以很好地获取IP地址,但我不知道如何获取它们的主机名。请问该在代码中做出什么改变。抱歉我的英语不好。
以下是我的代码,用于获取局域网中的IP地址
String connections = "";
InetAddress host;
try
{
host = InetAddress.getByName("192.168.1.1");
byte[] ip = host.getAddress();
for(int i = 1; i <= 254; i++)
{
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
if(address.isReachable(100))
{
System.out.println(address + " machine is turned on and can be pinged "+address.getCanonicalHostName());
connections+= address+"\n";
}
else if(!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address + " machine is known in a DNS lookup");
System.out.println(address.getHostAddress()+"host Name:"+ address.getHostName());
}
}
tv.setText(connections);
}
catch(UnknownHostException e1)
{
e1.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}