[Android]获取局域网广播地址的两种方法
2016-08-27 08:42
721 查看
[Android]获取局域网广播地址的两种方法
第一种是自己写的,有一些bug,不过凑合着用也可以,第二种是stackoverflow的,推荐用这种。(1)
private InetAddress calcBroadcastAddress(InetAddress mask, InetAddress ip) throws IOException { int ipaddress = getIntAddress(ip); int maskaddress = getIntAddress(mask); int broadcast = ipaddress & maskaddress | ~maskaddress; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) { quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); } return InetAddress.getByAddress(quads); } private InetAddress getBroadcastAddress() throws IOException { // 自动获取 String strIp = getCommandResult("/system/bin/getprop", "dhcp.eth0.ipaddress"); String strMask = getCommandResult("/system/bin/getprop", "dhcp.eth0.mask"); if (!strIp.equals("\n")) { InetAddress mask = InetAddress.getByName(strMask); InetAddress ip = InetAddress.getByName(strIp); Log.v("ws-discovery", "auto ip:" + strIp); return calcBroadcastAddress(mask, ip); } else { // 手动配置 String strManual = getCommandResult("/system/bin/ifconfig", "eth0"); String[] netInfo = strManual.split(" "); if (netInfo.length >= 5) { InetAddress mask = InetAddress.getByName(netInfo[4]); InetAddress ip = InetAddress.getByName(netInfo[2]); Log.v("ws-discovery", "manual ip:" + netInfo[2]); return calcBroadcastAddress(mask, ip); } } // wifi WifiManager wifi = (WifiManager) mContext .getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); Log.v("ws-discovery", "wifi ip:" + Integer.toString(dhcp.ipAddress)); 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); } private int getIntAddress(InetAddress address) { byte[] addrs = address.getAddress(); int addr = 0; addr = ((addrs[3] & 0xff) << 24) | ((addrs[2] & 0xff) << 16) | ((addrs[1] & 0xff) << 8) | (addrs[0] & 0xff); return addr; } private String getCommandResult(String commands, String args) { Process process = null; String inet = null; try { process = new ProcessBuilder().command(commands, args) .redirectErrorStream(true).start(); InputStream in = process.getInputStream(); int count = 0; while (count == 0) { count = in.available(); } byte[] b = new byte[count]; in.read(b); inet = new String(b); } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) process.destroy(); } return inet; }
(2)
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()) { if (interfaceAddress.getBroadcast() != null) { return interfaceAddress.getBroadcast().toString().substring(1); } } } } return null; }
相关文章推荐
- [Android]获取局域网广播地址的两种方法
- Android /java获取系统广播地址、ip地址、子网掩码的方法
- Android获取本机Mac地址及IP地址方法
- 转:获取网页URL地址及参数等的两种方法(js和C#)
- android获取屏幕宽高的两种方法
- Android获取本机Mac地址及IP地址方法
- android应用 获取本地指定类型文件 的两种最优方法
- 获取网页URL地址及参数等的两种方法(js和C#)
- android屏幕信息获取的两种方法
- 获取android连接wifi时候的广播地址
- Android_获取屏幕大小的两种方法
- android应用 获取本地指定类型文件 的两种最优方法
- android--由文件名获取文件Id的两种方法
- Android获取屏幕宽高的两种方法
- android 获取Bitmap 的两种方法
- 转:获取网页URL地址及参数等的两种方法(js和C#)
- android:获取本机Mac地址及IP地址方法
- 获取网页URL地址及参数等的两种方法(js和C#)
- android应用 获取本地指定类型文件 的两种最优方法
- android应用 获取本地指定类型文件 的两种最优方法