detecting ip adress

first time doing this, obviously, i need to find out clients IP adress. i’ve searched for like an hour now, but cant find java api that can give me internet adress of a client . or maby im just looking for wrong things. best i’ve found is to make php script that reads ip address but i hope there is a simpler way :frowning:

any help or ling is welcome, thanks!

Use java.net.InetAddress -> http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetAddress.html

We know about that, our (I’m working with him on a game) problem is we can’t detect router’s public IP aderss that has been given to it by ISP. Only thing we came up with is puting a PHP script, connecting to it and script returning us the IP, or UPnP which we’re trying to avoid since we don’t know a thing about it.

Where and why do you need to know this address? Do you really need to know the address on the client?

I general, I don’t think you can ever know the “external” IP address of the client on the client itself without connecting to an external server. If you’re going to connect to an external server anyway, it might as well be the real server, instead of some script intended solely to determine your IP address. The server will be able to get the client’s IP address from the connection/socket.

We need for connecting to MySql database and there to put IP in server_list.

Like someone hosts, program connects to MySql and puts his IP in server_list. Somebody else wants to connect and he retreives list of avaliable servers from database. That’s why we need IP. We connect directly to db. We’re going to need java prog on server also to detect IP and stores it in db? :frowning: Would be much simpler if we could just connect to db directly.

The client cannot reliably determine the public internet address (if it is behind a router). So you need to do this on the server, either via a PHP script or using a java servlet on the server. Case closed I’m afraid.

Alan :slight_smile:

i did find a way, detecting hostname from the database connection, seems like it works, thanks for da help!

yeah… but it’s database soulution, not quite related to java… anyway thanks, we’ll know for the future! :stuck_out_tongue:

import java.net.;
import java.io.
;

public class URLTest{

public static void main (String args[]) {
    URL u;
    String ip = null;	    
    try {
        u = new URL("http://www.whatismyip.org");
        try {
        	DataInputStream theHTML = new DataInputStream(u.openStream());	  
        	u.getContent();
        	ip = theHTML.readLine();
        	System.out.println(ip);
        }
        catch (Exception e) {
          System.err.println(e);
        }
      }
      catch (MalformedURLException e) {
        System.err.println(e);
      }	      
  }

}

easiest way to get your ip.

The .org page shows ONLY your ip, thus a simple read line gives you the first line on page (your ip).

You could loop through the enumeration of network interfaces (the NetworkInterfaces class somewhere) and get all their associated addresses.

This includes MAC addresses and I don’t know what, but it’s the only way I’ve found which actually works. If the computer has multiple IPs this should get them. However it probably won’t determine which IP another person should connect to if there’s a router involved :).