java 安卓广播地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2993874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Broadcast Address
提问by RailsSon
I am making a Client Server application for my Androidphone.
我正在为我的Android手机制作客户端服务器应用程序。
I have created a UDPServer in Pythonwhich sits and listens for connections.
我创建了一个UDP服务器,在Python其中坐下并侦听连接。
I can put either the server IPaddress in directly like 192.169.0.100and it sends data fine. I can also put in 192.168.0.255and it find the server on 192.169.0.100.
我可以IP直接输入服务器地址192.169.0.100,它可以很好地发送数据。我也可以输入192.168.0.255,它会在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 Wifinetwork or other Wifinetworks.
是否可以获取我的 Android 手机所连接网络的广播地址?我只会在我的Wifi网络或其他Wifi网络上使用此应用程序。
Cheers
干杯
采纳答案by Cristian
As the broadcast IP address is the current IP address but finishing with 255, you can do something like this:
由于广播 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;
}
回答by Sofi Software LLC
From
从
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.
这样做的好处是只看WIFI。我知道 OP 说“我只会在我的 Wifi 网络或其他 Wifi 网络上使用这个应用程序。” 但值得一提的是,以防其他人需要非 wifi 替代品。
回答by braden
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;
}
回答by user667522
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";
}
}

