Android 广播地址

发布于 2024-09-04 05:59:19 字数 369 浏览 7 评论 0原文

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

我在 Python 中创建了一个 UDP 服务器,它负责监听连接。

我可以直接输入服务器 IP 地址,例如 192.169.0.100 并且它可以正常发送数据。我还可以输入 192.168.0.255,它会在 192.169.0.100 上找到服务器。

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

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

憧憬巴黎街头的黎明 2024-09-11 05:59:19

来自

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

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.

聚集的泪 2024-09-11 05:59:19

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

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

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;
}
小嗷兮 2024-09-11 05:59:19

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

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

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";
    }
}
猥︴琐丶欲为 2024-09-11 05:59:19

由于广播 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文