gpt4 book ai didi

java - 安卓广播地址

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:08 25 4
gpt4 key购买 nike

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

我已经在 Python 中创建了一个 UDP 服务器,用于监听连接。

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

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

干杯

最佳答案

来自

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

关于java - 安卓广播地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2993874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com