tags:

views:

226

answers:

3

I set up a static IP and did port forwarding on my notebook, and now I have a static IP address, but it's relatively static, every time I re-start the machine, I get another address, and since I have a "static" IP I can now do Paypal IPN messaging. But how can I get this static IP from my Java program ? One way I can think of is to visit : http://portforward.com/ and on that page it tells me what my external IP is, so I can extract it with Java code, is there any other way that I can do in my Java code to get this info ?

+1  A: 

The best solution is probably dynamic DNS. Essentially, you run a program on your computer (or router) that notifies a DNS server when your IP changes. Then, you can just tell PayPal the domain name.

Matthew Flaschen
A: 

I see three ways of doing this:

  1. As you discovered, querying an external server what IP you're apparently connecting from. This is simple but you require such a service to be available and, usually, that no transparent proxy messes with your results.
  2. IGD, a sub-protocol of UPnP can give you the result very easily if your port forwarding devices supports it (many do). Google "IGD JAVA" for libraries and samples.
  3. Register for a dynamic DNS service and then lookup your own DNS name.
Stephane
How to "Register for a dynamic DNS service" ? Do I need to call my ISP, install a program, is it free ?
Frank
At its simplest, you just sign up for a Dynamic DNS service (such as http://www.dyndns.com/services/dns/dyndns/) and use whatever mechanism they have to update your real IP. Most (if not all) services offer a small tool you can run to update your Dynamic DNS name with your real IP automatically.DynDNS's service is free for up to 5 hostnames.
Matt
@Matt : thanks, very helpful ! I'm a bit confused though, so is it true that I don't even need to set up a static IP and do the port forwarding, just goto dyndns.com/services/dns/dyndns and register for their service and run their software, it will automatically send my dynamic address to my registered name ? What's the point of setting static IP and forward it on my own ?
Frank
A: 

You can use the NetworkInterface class:

Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
  NetworkInterface ifc = ifcs.nextElement();
  System.out.println("Adresses of: "+ifc.getDisplayName());
  Enumeration<InetAddress> adresses = ifc.getInetAddresses();
  while(adresses.hasMoreElements()){
    System.out.println(adresses.nextElement().getHostAddress());
  }
}

This snippet will show you all of the interfaces and the IPs bound to them. You will need to look through the list to find the appropriate interface (see also NetworkInterface.getByName(String name)) And then look at the addresses for that interface. Once you have found the appropriate InetAdress you can use that to get the string or byte representation.

M. Jessup
That will return the local address, not the public one.
Adam Crume