[英]How to get MAC Address without using IP Address in java socket programming
I am New in socket programming in Java.Can someone tell me , how to get MAC Address without using IP Address in socket programming. 我是Java中的套接字编程的新手。有人告诉我,如何在套接字编程中不使用IP地址获取MAC地址。
This is the code by which i can get MAC address in socket-- 这是我可以在socket中获取MAC地址的代码 -
NetworkInterface network = NetworkInterface.getByInetAddress(ip); NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "- " : ""));
}
System.out.println(sb.toString());
but this is using IP Address ultimately.Is there any way to get the MAC Address without using IP Address? 但这最终是使用IP地址。有没有办法在不使用IP地址的情况下获取MAC地址?
Any PC's MAC ADDRESS by using socket
使用套接字的任何PC的MAC ADDRESS
In a nutshell, there is no reliable method for finding out the MAC address of a host outside your subnet. 简而言之,没有可靠的方法来查找子网外主机的MAC地址。
If you are on the same subnet as the host in question, take a look at ARP and RARP . 如果您与相关主机位于同一子网,请查看ARP和RARP 。
MAC addresses are only used on local networks. MAC地址仅用于本地网络。 It is the way a switch/router knows where a packet has to be sent.
这是交换机/路由器知道数据包必须发送到何处的方式。 IP is used to transport packets from network to network.
IP用于将数据包从网络传输到网络。
All TCP/UDP packets include senders IP and MAC. 所有TCP / UDP数据包都包含发送方IP和MAC。 This way the receiving device can include the MAC in the return package so the switch/router know where to deliver it.
这样,接收设备可以在返回包中包括MAC,以便交换机/路由器知道将其传送到何处。 MAC addresses should be unique but there is no guarantee, and it is not possible to use as a device address on the internet.
MAC地址应该是唯一的,但不能保证,并且不能在互联网上用作设备地址。
When you use internet you use IP protocol (A global address system) When your on a local network the devices usually use MAC addresses. 使用Internet时,使用IP协议(全局地址系统)当您在本地网络上时,设备通常使用MAC地址。
try{
InetAddress ip = InetAddress.getLocalHost();
System.out.println("ip : " + ip);NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
String s=sb.toString();System.out.println(s);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.