8

我正在为我的Android手机制作客户端服务器应用程序。

我创建了一个UDP服务器,在Python其中坐下并监听连接。

我可以IP直接输入任何一个服务器地址,192.169.0.100它可以很好地发送数据。我也可以把192.168.0.255它找到服务器上192.169.0.100

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

干杯

4

4 回答 4

17

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 替代方案。

于 2013-02-08T21:06:53.583 回答
7

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

    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;
}
于 2012-03-28T17:16:07.267 回答
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";
    }
}
于 2017-01-21T17:33:03.723 回答
-2

由于广播 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;
}
于 2010-06-08T00:33:03.153 回答