android eth0 ipv4,android - How do I get IP_ADDRESS in IPV4 format - Stack Overflow

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..

public String getLocalIpAddress() {

try {

for (Enumeration en = NetworkInterface

.getNetworkInterfaces(); en.hasMoreElements();) {

NetworkInterface intf = en.nextElement();

for (Enumeration enumIpAddr = intf

.getInetAddresses(); enumIpAddr.hasMoreElements();) {

InetAddress inetAddress = enumIpAddr.nextElement();

System.out.println("ip1--:" + inetAddress);

System.out.println("ip2--:" + inetAddress.getHostAddress());

// for getting IPV4 format

if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

String ip = inetAddress.getHostAddress().toString();

System.out.println("ip---::" + ip);

EditText tv = (EditText) findViewById(R.id.ipadd);

tv.setText(ip);

// return inetAddress.getHostAddress().toString();

return ip;

}

}

}

} catch (Exception ex) {

Log.e("IP Address", ex.toString());

}

return null;

}

Added if condition as shown below

/**This shows IPV4 format IP address*/

if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){}

instead of this

/**This shows IPV6 format IP address*/

if (!inetAddress.isLoopbackAddress()){}

Many Thanks..

Rahul

An alternative for checking if the address is a version 4 address is:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分析下面的ebtables规则 # ebtables -L Bridge table: filter Bridge chain: INPUT, entries: 2, policy: ACCEPT -j br_wan -j portmapping_igmp Bridge chain: FORWARD, entries: 6, policy: DROP -j disBCMC -j br_wan_forward -j wlan_block -j wlan_map_block -j macfilter -j portmapping Bridge chain: OUTPUT, entries: 2, policy: ACCEPT -j br_wan_out -j br_lan_out Bridge chain: br_lan_out, entries: 2, policy: ACCEPT -p IPv6 -o eth0.4 --ip6-proto ipv6-icmp --ip6-icmp-type router-advertisement -j DROP -p IPv6 -o eth0.5 --ip6-proto ipv6-icmp --ip6-icmp-type router-advertisement -j DROP Bridge chain: br_wan_forward, entries: 3, policy: RETURN -p IPv6 -o nas0_5 -j ACCEPT -p PPP_SES -o nas0_5 -j ACCEPT -o nas0_5 -j DROP Bridge chain: wlan_block, entries: 0, policy: RETURN Bridge chain: wlan_map_block, entries: 0, policy: RETURN Bridge chain: disBCMC, entries: 2, policy: RETURN -d Broadcast -j DROP -d Multicast -j DROP Bridge chain: br_wan, entries: 1, policy: RETURN -i nas0_5 -j DROP Bridge chain: br_wan_out, entries: 1, policy: RETURN -o nas0_5 -j DROP Bridge chain: portmapping_igmp, entries: 2, policy: RETURN -p IPv4 -i eth0.4 --ip-proto igmp -j DROP -p IPv4 -i eth0.5 --ip-proto igmp -j DROP Bridge chain: macfilter, entries: 0, policy: ACCEPT Bridge chain: portmapping, entries: 31, policy: ACCEPT -i nas0_5 -o wlan1-vap3 -j RETURN -i wlan1-vap3 -o nas0_5 -j RETURN -i nas0_5 -o wlan1-vap2 -j RETURN -i wlan1-vap2 -o nas0_5 -j RETURN -i nas0_5 -o wlan1-vap1 -j RETURN -i wlan1-vap1 -o nas0_5 -j RETURN -i nas0_5 -o wlan1-vap0 -j RETURN -i wlan1-vap0 -o nas0_5 -j RETURN -i nas0_5 -o wlan1 -j RETURN -i wlan1 -o nas0_5 -j RETURN -i nas0_5 -o wlan0-vap3 -j RETURN -i wlan0-vap3 -o nas0_5 -j RETURN -i nas0_5 -o wlan0-vap2 -j RETURN -i wlan0-vap2 -o nas0_5 -j RETURN -i nas0_5 -o wlan0-vap1 -j RETURN -i wlan0-vap1 -o nas0_5 -j RETURN -i nas0_5 -o wlan0-vap0 -j RETURN -i wlan0-vap0 -o nas0_5 -j RETURN -i nas0_5 -o wlan0 -j RETURN -i wlan0 -o nas0_5 -j RETURN -i nas0_5 -o eth0.5 -j RETURN -i eth0.5 -o nas0_5 -j RETURN -i nas0_5 -o eth0.4 -j RETURN -i eth0.4 -o nas0_5 -j RETURN -i eth+ -o eth+ -j RETURN -i eth+ -o wlan+ -j RETURN -i wlan+ -o eth+ -j RETURN -i wlan+ -o wlan+ -j RETURN -i eth0+ -j DROP -i wlan+ -j DROP -i nas0_5 -j DROP
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值