0

When phone device connects on 3G network, How can I determine How to determine whether android device's ip address is public or not?

public static InetAddress getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress address = enumIpAddr.nextElement();
                return address;
            }
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
    }
    return null;
}
Matt
  • 22,721
  • 17
  • 71
  • 112
sweetier
  • 131
  • 1
  • 3
  • 11
  • What is it you're trying to do? – Falmarri Dec 08 '10 at 00:44
  • 2
    An Android device will pretty much never have a public IP address, just as most PCs pretty much never have a public IP address. Everything is behind NAT, partly due to limited IPv4 address space, partly for security. Now, if somebody puts Android on a router, then perhaps it'd have a public IP address, but that is such a fringe use case right now that I hardly think it would be worth worrying about. – CommonsWare Dec 08 '10 at 00:47
  • i am trying to run serversocket – sweetier Dec 08 '10 at 00:47
  • 1
    Check the address range. http://www.vicomsoft.com/glossary/addresses.html – SRM Dec 08 '10 at 01:24
  • My Android phone on Three UK optionally has a public IP address. (Whether it does or not depends which APN I use.) – RichieHindle Jun 10 '11 at 15:02

1 Answers1

0

Write a quick little webpage like this:

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

Then fetch that with an async task:

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

public class NetreadTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
        return netread(urls[0]);
    }

    public final String netread(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            ResponseHandler<String> resHandler = new BasicResponseHandler();
            String page;
            page = httpClient.execute(httpGet, resHandler);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

Call it like:

String publicip = new NetreadTask().exec([yourpage]).get();

Then compare the two. The ip you get via the method above is guaranteed to give you the public ip address.

I know this is an old question but I hope it helps somebody!

Osmium USA
  • 1,751
  • 19
  • 37