Android Broadcast Address
Asked Answered
S

4

8

I am making a Client Server application for my Android phone.

I have created a UDP Server in Python which sits and listens for connections.

I can put either the server IP address in directly like 192.169.0.100 and it sends data fine. I can also put in 192.168.0.255 and it find the server on 192.169.0.100.

Is it possible to get the broadcast address of the network my Android phone is connected to? I am only ever going to use this application on my Wifi network or other Wifi networks.

Cheers

Staurolite answered 7/6, 2010 at 23:26 Comment(0)
T
-2

As the broadcast IP address is the current IP address but finishing with 255, you can do something like this:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {}
    return null;
}

public static String getBroadcast() throws SocketException {
    System.setProperty("java.net.preferIPv4Stack", "true");
    for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum.hasMoreElements();) {
        NetworkInterface ni = niEnum.nextElement();
        if (!ni.isLoopback()) {
            for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                return interfaceAddress.getBroadcast().toString().substring(1);
            }
        }
    }
    return null;
}
Trioecious answered 8/6, 2010 at 0:33 Comment(5)
That's making the (possibly unwarranted) assumption that the netmask is 255.255.255.255. You should actually get the netmask associated with the address that you retrieved, and then compute bcast = ipAddress | ~netmaskBastogne
I was thinking that, how would you implement such a thing? CheersStaurolite
"As the broadcast IP address is the current IP address but finishing with 25" ... no. a typical home network has a bcast addr of .255, but that's not true in general.Unhesitating
A class C subnet 192.168.0.1 -> 192.168.0.126 with subnet mask of 255.255.255.128 has a broadcast address of: 192.168.0.127Hellraiser
Thank you, I'll upvote your answer when you remove: "As the broadcast IP address is the current IP address but finishing with 255"Hellraiser
C
18

From

http://code.google.com/p/boxeeremote/source/browse/trunk/Boxee+Remote/src/com/andrewchatham/Discoverer.java?spec=svn28&r=28

private InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
      quads[k] = (byte) (broadcast >> (k * 8));
    return InetAddress.getByAddress(quads);
}

This has the advantage of only looking at WIFI. I know OP said "I am only ever going to use this application on my Wifi network or other Wifi networks." but it's worth mentioning this in case someone else needs a non-wifi alternative.

Comma answered 8/2, 2013 at 21:6 Comment(1)
and... don't forget your manifest permission folks! <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>Hokusai
H
7

Here is a method that should work:

    public static String getBroadcast(){
    String found_bcast_address=null;
     System.setProperty("java.net.preferIPv4Stack", "true"); 
        try
        {
          Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
          while (niEnum.hasMoreElements())
          {
            NetworkInterface ni = niEnum.nextElement();
            if(!ni.isLoopback()){
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses())
                {

                  found_bcast_address = interfaceAddress.getBroadcast().toString();
                  found_bcast_address = found_bcast_address.substring(1);

                }
            }
          }
        }
        catch (SocketException e)
        {
          e.printStackTrace();
        }

        return found_bcast_address;
}
Hellraiser answered 28/3, 2012 at 17:16 Comment(1)
It throws an exception when IPv6 also used, because it has no broadcast address and getBroadcast() returns null, toString() throws exception. (At least on my windows emulator.) Easy to fix, just check if interfaceAddress.getBroadcast() is null or not.Worst
H
0

A simpler way perhaps ...

public static String getBroadcast() throws Exception {
    System.setProperty("java.net.preferIPv4Stack", "true");
    InetAddress inet = InetAddress.getLocalHost();
    NetworkInterface net = NetworkInterface.getByInetAddress(inet);
    InterfaceAddress [] interfaceAddresses = net.getInterfaceAddresses().toArray(new InterfaceAddress[0]);
    if ( interfaceAddresses.length > 0 ) {
        return interfaceAddresses[0].getBroadcast().toString().substring(1);
    } else {
        return "255.255.255";
    }
}
Hb answered 21/1, 2017 at 17:33 Comment(1)
Shouldn't it be "255.255.255.255"?Rind
T
-2

As the broadcast IP address is the current IP address but finishing with 255, you can do something like this:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {}
    return null;
}

public static String getBroadcast() throws SocketException {
    System.setProperty("java.net.preferIPv4Stack", "true");
    for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum.hasMoreElements();) {
        NetworkInterface ni = niEnum.nextElement();
        if (!ni.isLoopback()) {
            for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                return interfaceAddress.getBroadcast().toString().substring(1);
            }
        }
    }
    return null;
}
Trioecious answered 8/6, 2010 at 0:33 Comment(5)
That's making the (possibly unwarranted) assumption that the netmask is 255.255.255.255. You should actually get the netmask associated with the address that you retrieved, and then compute bcast = ipAddress | ~netmaskBastogne
I was thinking that, how would you implement such a thing? CheersStaurolite
"As the broadcast IP address is the current IP address but finishing with 25" ... no. a typical home network has a bcast addr of .255, but that's not true in general.Unhesitating
A class C subnet 192.168.0.1 -> 192.168.0.126 with subnet mask of 255.255.255.128 has a broadcast address of: 192.168.0.127Hellraiser
Thank you, I'll upvote your answer when you remove: "As the broadcast IP address is the current IP address but finishing with 255"Hellraiser

© 2022 - 2024 — McMap. All rights reserved.