0

I have a service that listens for a connection and I want it to listen only on a specific network. I can get my device's IP addresses like this:

    private void printNetworkInterfaces()
    {
        try
        {
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface ni : Collections.list(nis))
            {
                Enumeration<InetAddress> iis = ni.getInetAddresses();

                for (InetAddress ia : Collections.list(iis))
                {
                    // We only want IPv4 addresses
                    if (!isIPv4(ia.getHostAddress()))
                        continue;
                    // Do tomething with ia.getHostAddress();


                }
            }    
        }
        catch (SocketException e)
        {
            Log.w(TAG,e);
        }
    }

But instead of the IP of the device, i want the IP of the network to listen on that network. For example, if the IP were 192.168.1.1, then the network IP should be 192.168.1.0.

I could replace the 4th value with a 0 directly, but this would fail for other networks.

Is there some way to get the network address associated to the IP addres returned by InetAddress.getHostAddress(); ?

Twinone
  • 2,949
  • 4
  • 28
  • 39
  • possible duplicate of [How can i get Ip addresses of other Wifi enabled devices by programmatically on the same network?](http://stackoverflow.com/questions/12103898/how-can-i-get-ip-addresses-of-other-wifi-enabled-devices-by-programmatically-on) – Tofeeq Ahmad Jan 12 '13 at 12:48
  • He's asking for other IP's on the network. I'm asking for the IP address of the network itself. – Twinone Jan 12 '13 at 13:02

1 Answers1

1

From network mask and ip you can calculate the network ip.

wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
 d=wifii.getDhcpInfo();
 s_dns1="DNS 1: "+String.valueOf(d.dns1);
 s_dns2="DNS 2: "+String.valueOf(d.dns2);    
 s_gateway="Default Gateway: "+String.valueOf(d.gateway);    
 s_ipAddress="IP Address: "+String.valueOf(d.ipAddress); 
 s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);     
 s_netmask="Subnet Mask: "+String.valueOf(d.netmask);    
 s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);
Totoo
  • 139
  • 11
  • How would I calculate this? Also, the DhcpInfo is only in WifiManager and not in ConnectivityManager. – Twinone Jan 12 '13 at 13:00
  • Simply AND the mask bits ant the ip bits. http://en.wikipedia.org/wiki/Subnetwork or use this: http://stackoverflow.com/questions/1221517/how-to-get-subnet-mask-using-java – Totoo Jan 12 '13 at 13:28
  • Only return internal IP – Pedro Paulo Amorim Sep 08 '14 at 19:40
  • That was the question. If you need the external IP, check this example: https://code.google.com/p/external-ip/source/browse/src/org/kost/externalip/ExternalIP.java . this is just an example, you should get a reliable provider, and process the string you download from the wbepage. (eg skip ads, ...) – Totoo Sep 08 '14 at 19:57