我正在为我的安卓
手机客户端服务器应用程序。
I am making a Client Server application for my Android
phone.
我在的Python
它位于和侦听连接创建一个 UDP
服务器。
I have created a UDP
Server in Python
which sits and listens for connections.
我可以把任一服务器 IP
直接解决诸如 192.169.0.100
,并将其发送数据的罚款。我也可以把 192.168.0.255
并找到服务器 192.169.0.100
。
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
.
是否有可能让我的Android手机连接到网络的广播地址?我只是以往任何时候都使用此应用程序我的无线
网络或其他无线
网络上。
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.
干杯
随着广播IP地址是当前的IP地址,但随着255完成,你可以做这样的事情:
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;
}