1

Is there a way to receive notification in Java when IP address changes?

For example when the machine receives a new address from a dhcp server. I would like to avoid polling as the machine is already under heavy load. From what I found so far it seems impossible.

Something that works in pure java would be best but a linux specific way would be fine too.

Edit: I should have mentioned that the IP address is that of the local machine, not some remote server. Ie. I would like to receive notification when the IP address of the machine where the java program is running is changed.

Prvaak
  • 757
  • 1
  • 6
  • 13
  • What are your constraints ? Maybe something like DynDNS, or use a fixed IP can help you. – NiziL Jan 13 '15 at 09:30
  • The constraint is that the IP address of the machine may change and I cannot control it. My goal is simply to display it on screen. But I need the address to be current. Checking it every second would be enough but it goes against my programming principles to check something in a while loop :) – Prvaak Jan 13 '15 at 09:50
  • But checking in a loop is the easiest way. first step is to solve your problem and after that you could look for a better solution. most of the time you do not need a better solution. – Niels Jan 13 '15 at 09:54

4 Answers4

0

Linux specific way: Write a shell script to monitor syslog for recent changes. Extract IP address if there is any change from syslog(/var/log/syslog) or u can use run ifconfig perodically and compare id there is any chage in IP address.

manish
  • 944
  • 2
  • 14
  • 27
0

You can use this tool to generate a trigger that get's picked up by your Java program:

http://sourceforge.net/projects/ipmonitor/

Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
0

I don't think you do it without polling. The problem is that in the general case, the machine might have several IPs, which complicates things.

Assuming you're interested in the IP used to connect to some known location (e.g. some internal server or just plainly "www.google.com"), the easiest way it to open a socket to that location and call socket.getLocalAddress(). That will always return the correct IP address. This solution is cross platform and easy to implement, but kinda awkward if you don't really need that connection.

Another approach is parsing the operating system's routing table (on linux run route -en. This doesn't require external connections, but does require (sometimes intricate) parsing.

A third, cleaner, approach of getting the current IP address is outlined here. But it's problematic when the program runs on a machine with more than one public ip, or one which has multiple private addresses and no public ones. With this approach, you end up guessing the IP in such cases, and you might not always be right. But if you know which address you want to monitor, or know the interface name, this is by far the best solution.

Community
  • 1
  • 1
Malt
  • 28,965
  • 9
  • 65
  • 105
-1

Try this:

InetAddress old = InetAddress.getLocalHost();
while(true) {
    InetAddress new = InetAddress.getLocalHost();
    if(!old.toString().equals(new.toString())) {
        System.out.println("IP has changed to: " + new.toString());
        System.out.println("IP was: " + old.toString());
        old = new;
    }
}

You should run this loop within a seperate Thread and change while(true) into a boolean field which you can manage from outside the thread

Niels
  • 302
  • 1
  • 2
  • 9
  • That's a bad solution. The machine might have several IPs, and the one that changed could be different from the one returned by getLocalHost() – Malt Jan 13 '15 at 10:05