使用Java的远程PC名称和IP

使用Java的远程PC名称和IP,java,Java,我正在尝试使用Java在网络中连接远程PC名称和IP地址。我可以获取IP,但无法获取设备的名称。我附上代码以供参考 Java代码: import java.io.IOException; import java.net.InetAddress; public class findIP { public static void main(String[] args) throws IOException { InetAddress localhost = InetA

我正在尝试使用Java在网络中连接远程PC名称和IP地址。我可以获取IP,但无法获取设备的名称。我附上代码以供参考

Java代码:

import java.io.IOException;
import java.net.InetAddress;

public class findIP {


    public static void main(String[] args) throws IOException {

        InetAddress localhost = InetAddress.getLocalHost();
        // this code assumes IPv4 is used
        byte[] ip = localhost.getAddress();
        String N = localhost.getHostName();

        for (int i = 1; i <= 254; i++)
        {
            ip[3] = (byte)i;
            InetAddress address = InetAddress.getByAddress(ip);
            InetAddress name = InetAddress.getByName(N);
        if (address.isReachable(1000))
        {
            System.out.println(address+"            " +"Device Name: "+name);
        }
import java.io.IOException;
导入java.net.InetAddress;
公共类findIP{
公共静态void main(字符串[]args)引发IOException{
InetAddress localhost=InetAddress.getLocalHost();
//此代码假定使用了IPv4
字节[]ip=localhost.getAddress();
字符串N=localhost.getHostName();
对于(int i=1;i替换

InetAddress name=InetAddress.getByName(N);

String hostName=address.getHostName();

调用
getByName
得到的是IP地址,而不是主机名。请阅读。

替换

InetAddress name=InetAddress.getByName(N);

String hostName=address.getHostName();

通过调用
getByName
得到的是IP地址,而不是主机名。请阅读。

这应该可以

 InetAddress host = InetAddress.getByName("xxx.xxx.xxx.xxx");
 System.out.println(host.getHostName());
这应该可以

 InetAddress host = InetAddress.getByName("xxx.xxx.xxx.xxx");
 System.out.println(host.getHostName());

首先,
InetAddress.getByName(N)
正在尝试解析服务器主机名,而不是客户端。因此,您的代码应该反映您的意图:

String name = address.getHostName();

现在回答您的问题,
InetAddress.getHostName()
将返回相应IP地址的反向DNS查找结果。并非应用程序的所有客户端都有DNS记录,因此您可能不会看到“设备名”。首先,
InetAddress.getByName(N)
正在尝试解析您的服务器主机名,而不是客户端。因此,您的代码应该反映您的意图:

String name = address.getHostName();


现在回答您的问题,
InetAddress.getHostName()
将返回相应IP地址的反向DNS查找结果。并非应用程序的所有客户端都有DNS记录,因此您可能看不到“设备名”。

您是指windows PC计算机名吗?请尝试
address.getHostName()
。我想localhost.getHostName()返回主机名和ip。@zack6849是的,我是说远程PC计算机名。@VHS尝试过,但它说仍然给我自己的计算机名。你是说,像windows PC计算机名吗?试试
地址。getHostName()
。我想localhost.getHostName()返回主机名和ip。@zack6849是的,我是指远程PC计算机名。@VHS尝试过,但它说仍然给我自己的计算机名给我输出:/10.10.10.10设备名:xxx.xxx.xxx.xxx给我输出:/10.10.10.10设备名:xxx.xxx.xxx获得了同时使用名称和ip的代码。还有一个问题是如何获得地址和要分离的名称。当我使用{System.out.println(地址+++”名称:“+hostName”)运行代码时,我得到的输出是:“devicename/ip名称:devicename”。恐怕我不在您的问题范围内。语句
System.out.println(地址+”“+”名称:“+hostName”);
不会打印IP地址。输出:canonc4403f.intellig.local/10.41.27.11设备名称:canonc4403f.intellig.local以上是我运行代码时得到的输出。因为我有一个设备连接到网络。如何使IP与设备名称分离。显示打印上述输出的
sysout
语句ut.get代码与name和IP一起工作。还有一个问题是如何将地址和名称分离。当我使用{System.out.println(address++name:+hostName)}运行代码时,我得到的输出是:“devicename/IP name:devicename”我恐怕不在你的问题下。语句
System.out.println(地址+“”+”名称:“+主机名);
不会打印IP地址。输出:canonc4403f.intellig.local/10.41.27.11设备名称:canonc4403f.intellig.local以上是我运行代码时得到的输出。因为我有一个设备连接到网络。如何使IP与设备名称分离。显示打印上述输出的
sysout
语句美国犹他州。