android 网络获取地址,android 网络地址,ip地址获取,子网掩码获取,默认网关获取...

获取eth0的ip: getIpAddrForInterfaces("eth0")java

private String getIpAddrForInterfaces(String interfaceName){

try {

Enumeration enNetworkInterface = NetworkInterface.getNetworkInterfaces(); //获取本机全部的网络接口

while (enNetworkInterface.hasMoreElements()) { //判断 Enumeration 对象中是否还有数据

NetworkInterface networkInterface = enNetworkInterface.nextElement(); //获取 Enumeration 对象中的下一个数据

if (!networkInterface.isUp()) { // 判断网口是否在使用

continue;

}

if (!interfaceName.equals(networkInterface.getDisplayName())) { // 网口名称是否和须要的相同

continue;

}

Enumeration enInetAddress = networkInterface.getInetAddresses(); //getInetAddresses 方法返回绑定到该网卡的全部的 IP 地址。

while (enInetAddress.hasMoreElements()) {

InetAddress inetAddress = enInetAddress.nextElement();

if (inetAddress instanceof Inet4Address) { //判断是否未ipv4

return inetAddress.getHostAddress();

}

// 判断未lo时

// if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {

// return inetAddress.getHostAddress();

// }

}

}

} catch (Exception e) {

e.printStackTrace();

}

return "error";

}

获取eth0的子网掩码:getIpAddrMaskForInterfaces("eth0")网络

private String getIpAddrMaskForInterfaces(String interfaceName) {

try {

Enumeration networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); //获取本机全部的网络接口

while (networkInterfaceEnumeration.hasMoreElements()) { //判断 Enumeration 对象中是否还有数据

NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); //获取 Enumeration 对象中的下一个数据

if (!networkInterface.isUp() && !interfaceName.equals(networkInterface.getDisplayName())) { //判断网口是否在使用,判断是否时咱们获取的网口

continue;

}

for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { //

if (interfaceAddress.getAddress() instanceof Inet4Address) { //仅仅处理ipv4

return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength()); //获取掩码位数,经过 calcMaskByPrefixLength 转换为字符串

}

}

}

} catch (SocketException e) {

e.printStackTrace();

}

return "error";

}

//经过子网掩码的位数计算子网掩码

public static String calcMaskByPrefixLength(int length) {

int mask = 0xffffffff << (32 - length);

int partsNum = 4;

int bitsOfPart = 8;

int maskParts[] = new int[partsNum];

int selector = 0x000000ff;

for (int i = 0; i < maskParts.length; i++) {

int pos = maskParts.length - 1 - i;

maskParts[pos] = (mask >> (i * bitsOfPart)) & selector;

}

String result = "";

result = result + maskParts[0];

for (int i = 1; i < maskParts.length; i++) {

result = result + "." + maskParts[i];

}

return result;

}

获取默认网关:getGateWay() oop

private String getGateWay() {

String [] arr;

try {

Process process = Runtime.getRuntime().exec("ip route list table 0");

String data = null;

BufferedReader ie = new BufferedReader(new InputStreamReader(process.getErrorStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

String string = in.readLine();

arr = string.split("\\s+");

return arr[2];

} catch (IOException e) {

e.printStackTrace();

}

return "error";

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值