Java - find host by MAC address

I have a Java / Android application that should send some data to a server on a local network. the problem is that the server ip address is dynamic, so the only way to find it is to find its MAC address. Is this possible in Java? Can you see other options?

+5
source share
3 answers

Searching for a host by MAC address can only work on your local network. Mac addresses are a single layer under IP addresses. Other networks do not have MAC address based routing.

Broadcast / Multicast

, , . , , , . - , , , mulitcast, . Multicast, , , , , . (, ), .

DNS

IP-, serevr. IP- , . . , , .

DNS- DNS- dyndns. DNS. DNS , /, . , -, - http/web, .

+2

: ,

arp, :

public static String getIpFromArpCache(String macaddr) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && macaddr.equals(splitted[3])) {
                // Basic sanity check
                String ip = splitted[0];
                if (validateIp(ip)) {
                    return ip;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

private static final String PATTERN = 
    "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

public static boolean validateIp(final String ip){          

      Pattern pattern = Pattern.compile(PATTERN);
      Matcher matcher = pattern.matcher(ip);
      return matcher.matches();             
}

+2

Java?

Java.

, MAC- IP-. ARP ( Linux/UNIX) ARP IP- MAC-. AFAIK, ARP Java.

, ( Linux) - "arpd -l" . ARP- , Java- . , MAC- IP-. , ARP , .


It is better to assign a static IP address to the server ... and a DNS address. Another possibility would be to have a server register using the Dynamic DNS service .

0
source

All Articles