Java Socket 获取 MAC 地址
在 Java 中,通过 Socket 获取 MAC 地址通常需要结合网络接口和 IP 地址信息,以下是几种实现方法:

通过 InetAddress 获取网络接口
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class MacAddressViaSocket {
public static void main(String[] args) {
try {
// 获取本地主机地址
InetAddress localHost = InetAddress.getLocalHost();
// 获取网络接口
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
if (networkInterface != null) {
byte[] macBytes = networkInterface.getHardwareAddress();
if (macBytes != null) {
StringBuilder sb = new StringBuilder();
for (byte b : macBytes) {
sb.append(String.format("%02X:", b));
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1); // 移除最后的冒号
}
System.out.println("MAC Address: " + sb.toString());
} else {
System.out.println("MAC address not found");
}
} else {
System.out.println("Network interface not found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
通过 Socket 连接获取远程 MAC 地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.UnknownHostException;
public class RemoteMacAddress {
public static void main(String[] args) {
String remoteHost = "192.168.1.1"; // 替换为目标IP
int port = 80; // 替换为目标端口
try {
// 创建Socket连接
Socket socket = new Socket(remoteHost, port);
InetAddress address = socket.getInetAddress();
// 获取网络接口
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] macBytes = ni.getHardwareAddress();
if (macBytes != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < macBytes.length; i++) {
sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? ":" : ""));
}
System.out.println("Remote MAC Address: " + sb.toString());
} else {
System.out.println("MAC address not found for " + remoteHost);
}
} else {
System.out.println("Network interface not found for " + remoteHost);
}
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意事项
- 权限问题:获取 MAC 地址可能需要管理员/root权限
- 虚拟网络适配器:某些系统可能有多个网络接口,需要正确选择
- 远程 MAC 地址限制:通常只能获取同一局域网内设备的MAC地址
- IPv6 支持:上述代码主要针对IPv4,如需支持IPv6需要额外处理
- 跨平台兼容性:不同操作系统可能返回不同格式的MAC地址
替代方案:使用 ARP 表
对于远程设备,可以尝试解析ARP表(仅限同一局域网):
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ArpMacResolver {
public static void main(String[] args) {
String ip = "192.168.1.1"; // 目标IP
try {
// Windows系统
Process process = Runtime.getRuntime().exec("arp -a " + ip);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
Pattern pattern = Pattern.compile("([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})");
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println("MAC Address from ARP: " + matcher.group());
break;
}
}
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
方法提供了在Java中通过Socket获取MAC地址的不同实现方式,您可以根据具体需求选择适合的方案。

