Android广播地址

时间:2010-06-07 23:26:04

标签: java android udp broadcast android-networking

我正在为Android电话制作客户端服务器应用程序。

我在UDP中创建了一个Python服务器,该服务器位于并侦听连接。

我可以将服务器IP地址直接放入192.169.0.100,并且可以正常发送数据。我也可以放入192.168.0.255并在192.169.0.100找到服务器。

是否可以获取我的Android手机所连接的网络的广播地址?我只会在Wifi网络或其他Wifi网络上使用此应用程序。

干杯

4 个答案:

答案 0 :(得分:15)

来自

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);
}

这样做的好处就是只看WIFI。我知道OP说“我只会在我的Wifi网络或其他Wifi网络上使用这个应用程序。”但值得一提的是,如果其他人需要非wifi替代品。

答案 1 :(得分:6)

这是一个应该有效的方法:

    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;
}

答案 2 :(得分:0)

由于广播IP地址是当前的IP地址,但以255结尾,您可以执行以下操作:

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;
}

答案 3 :(得分:0)

也许更简单的方式......

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";
    }
}