我有一个安卓手机网络,其中一部手机充当服务器。我尝试使用目标 IP 作为广播'255.255.255.255.'
。但它无法发送消息。我在下面给出logcat。服务器 IP 为192.168.43.1
. 我也试过了'192.168.43.255'
。但也以徒劳而告终。对于代码,我参考了http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
05-30 20:17:40.970: E/ClientActivity(698): java.net.UnknownHostException: /192.168.43.255
05-30 20:17:40.970: E/ClientActivity(698): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
05-30 20:17:40.970: E/ClientActivity(698): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
05-30 20:17:40.970: E/ClientActivity(698): at java.net.InetAddress.getByName(InetAddress.java:325)
05-30 20:17:40.970: E/ClientActivity(698): at project.android.aec.ConnectActivity$ClientThread.run(ConnectActivity.java:139)
05-30 20:17:40.970: E/ClientActivity(698): at java.lang.Thread.run(Thread.java:1019)
以下是用于获取广播IP的代码
private InetAddress getBroadcastAddress() throws IOException {
mWifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = mWifi.getDhcpInfo();
if (dhcp == null) {
Log.d("BCast Address Fetch", "Could not get dhcp info");
return null;
}
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) & 0xFF);
return InetAddress.getByAddress(quads);
}