Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用IP地址查找mac地址_Java_Sockets_Url - Fatal编程技术网

在java中使用IP地址查找mac地址

在java中使用IP地址查找mac地址,java,sockets,url,Java,Sockets,Url,我在查找远程主机的MAC地址时遇到问题,但我能够找到本地主机的MAC地址。如果我有其他系统的IP地址,我可以检索该系统的MAC地址吗 InetAddress=InetAddress.getByName192.168.46.53 如果我在工作组中指定系统的ip地址。。。ni值为空。。。。而无法取回它。。。。但是如果给我的系统的ip地址…它会取 谢谢, Sunny您只能获取本地LAN上远程主机的MAC地址,即与您的计算机位于同一子网中的主机。无法确定距离IP跃点(而非以太网跃点)超过一个跃点的主机的

我在查找远程主机的MAC地址时遇到问题,但我能够找到本地主机的MAC地址。如果我有其他系统的IP地址,我可以检索该系统的MAC地址吗


InetAddress=InetAddress.getByName192.168.46.53

如果我在工作组中指定系统的ip地址。。。ni值为空。。。。而无法取回它。。。。但是如果给我的系统的ip地址…它会取

谢谢,
Sunny

您只能获取本地LAN上远程主机的MAC地址,即与您的计算机位于同一子网中的主机。无法确定距离IP跃点(而非以太网跃点)超过一个跃点的主机的MAC地址


请注意,获取本地LAN上主机的相应MAC地址需要获取ARP表或发送和接收原始数据包所需的权限。大多数操作系统都允许在没有特殊权限的情况下读取ARP表,但用于读取ARP表的机制将根据操作系统的不同而有所不同。如果您需要特定操作系统的技术,则必须更新您的问题以包含该信息。

您只能获取本地LAN上远程主机的MAC地址,即与您的计算机位于同一子网中的主机。无法确定距离IP跃点(而非以太网跃点)超过一个跃点的主机的MAC地址


请注意,获取本地LAN上主机的相应MAC地址需要获取ARP表或发送和接收原始数据包所需的权限。大多数操作系统都允许在没有特殊权限的情况下读取ARP表,但用于读取ARP表的机制将根据操作系统的不同而有所不同。如果您需要特定操作系统的技术,您必须更新您的问题以包含该信息。

您认为您为什么应该能够获取不在本地子网中的远程主机的Mac地址。它不是IP协议所要求或通信的东西。您所能获得的最好信息是网关的Mac地址。netAddress address=InetAddress.getByName192.168.46.53;如果我在工作组中指定系统的ip地址。。。ni值为空。。。。而无法取回它。。。。但是如果给我的系统的ip地址…它会获取???为什么你认为你应该能够获取不在本地子网中的远程主机的Mac地址呢。它不是IP协议所要求或通信的东西。您所能获得的最好信息是网关的Mac地址。netAddress address=InetAddress.getByName192.168.46.53;如果我在工作组中指定系统的ip地址。。。ni值为空。。。。而无法取回它。。。。但是如果给我的系统的ip地址…它会取???
public static void main(String[] args) {
    try {
        InetAddress address = InetAddress.getLocalHost();
        // InetAddress address = InetAddress.getByName("192.168.46.53");

        /*
         * Get NetworkInterface for the current host and then read the
         * hardware address.
         */
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                /*
                 * Extract each array of mac address and convert it to hexa with the
                 * following format 08-00-27-DC-4A-9E.
                 */
                for (int i = 0; i < mac.length; i++) {
                    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                }
            } else {
                System.out.println("Address doesn't exist or is not accessible.");
            }
        } else {
            System.out.println("Network Interface for the specified address is not found.");
        }