【发布时间】:2013-01-12 12:43:22
【问题描述】:
我有一个侦听连接的服务,我希望它只侦听特定网络。我可以像这样获取设备的 IP 地址:
private void printNetworkInterfaces()
{
try
{
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface ni : Collections.list(nis))
{
Enumeration<InetAddress> iis = ni.getInetAddresses();
for (InetAddress ia : Collections.list(iis))
{
// We only want IPv4 addresses
if (!isIPv4(ia.getHostAddress()))
continue;
// Do tomething with ia.getHostAddress();
}
}
}
catch (SocketException e)
{
Log.w(TAG,e);
}
}
但不是设备的 IP,我希望网络的 IP 在该网络上进行侦听。 例如,如果 IP 是 192.168.1.1,那么网络 IP 应该是 192.168.1.0。
我可以直接用 0 替换第 4 个值,但这对于其他网络会失败。
有没有办法获取与 InetAddress.getHostAddress() 返回的 IP 地址关联的网络地址; ?
【问题讨论】:
-
他正在请求网络上的其他 IP。我要的是网络本身的 IP 地址。
标签: android networking ip