Alexander
Alexander

Reputation: 73

Make Java report a IPv6 address insted of IPv4

For a project in Java, i use a users ip address as a unique identifier for a website. It all works fine when a user only has a IPv4 address.

But here's my problem, when a user has a IPv6 address, Java will report a IPv4 address, while their browser will report a IPv6 address (In the $_SERVER['REMOTE_ADDR'] call in PHP).

So how do i make Java report it's IPv6 address insted of IPv4 ?

Edit: Some clarification:

This is only used to allow one person to download a file. It does not matter if several people use the same PC, not really important.

Say a user goes to http://whatismyipaddress.com/ with chrome, it will display a IPv6 address.

But if i now use Java to fetch http://whatismyipaddress.com/ it will show a IPv4 address. Why is this and how can i make it show the IPv6 address shown in Chrome ?

Edit 2: The code i use

public static String get(String url) throws WebFetchException {
    String result = "";
    try {
            URL url1 = new URL(url);
            HttpsURLConnection urlConn = (HttpsURLConnection) url1.openConnection();
            urlConn.setConnectTimeout(8 * 1000);
            urlConn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String text;
            while ((text = in.readLine()) != null) {
                result = result + text;
            }
            in.close();
        }
    } catch (Exception e) {
        throw new WebFetchException("There was an exception while fetching the requested page: " + url);
    }
    return result;
}

Upvotes: 0

Views: 324

Answers (1)

Sander Steffann
Sander Steffann

Reputation: 9978

The assumption that an ip address can be used as an identifier is wrong. More and more ISPs are deploying some form of NAT so many users will share a pool of IPv4 assesses. The address that a user is coming from can change from one connection to the other, so you can't rely on it being stable.

On the other hand more users also get IPv6, and devices have multiple IPv6 addresses that will change over time.

It also happens that because of connectivity issues users will switch between IPv4 and IPv6. Well, the user won't even notice, their device will just do it.

So relying on ip addresses just won't work.

Upvotes: 1

Related Questions