gpt4 book ai didi

android - 如何在android中获取连接到同一个Wi-Fi网络的系统的所有IP地址

转载 作者:太空宇宙 更新时间:2023-11-03 13:28:45 29 4
gpt4 key购买 nike

谁能告诉我如何在 Android 中以编程方式查找连接到同一网络的所有系统的 IP 地址。 DHCPInfo 类仅提供分配给我们的 android 设备的 IP 地址,而不提供连接到同一网络的其他设备的 IP 地址。如何获取连接到同一网络的其他设备的ipaddress?

最佳答案

首先通过这个获取主机ip

public String   s_dns1 ;
public String s_dns2;
public String s_gateway;
public String s_ipAddress;
public String s_leaseDuration;
public String s_netmask;
public String s_serverAddress;
TextView info;
DhcpInfo d;
WifiManager wifii;

wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE);
d = wifii.getDhcpInfo();

s_dns1 = "DNS 1: " + String.valueOf(d.dns1);
s_dns2 = "DNS 2: " + String.valueOf(d.dns2);
s_gateway = "Default Gateway: " + String.valueOf(d.gateway);
s_ipAddress = "IP Address: " + String.valueOf(d.ipAddress);
s_leaseDuration = "Lease Time: " + String.valueOf(d.leaseDuration);
s_netmask = "Subnet Mask: " + String.valueOf(d.netmask);
s_serverAddress = "Server IP: " + String.valueOf(d.serverAddress);

d.dns1是主机ip

现在通过这个获取连接的ips

String connections = "";
InetAddress host;
try
{
host = InetAddress.getByName(intToIp(d.dns1));
byte[] ip = host.getAddress();

for(int i = 1; i <= 254; i++)
{
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
if(address.isReachable(100))
{
System.out.println(address + " machine is turned on and can be pinged");
connections+= address+"\n";
}
else if(!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address + " machine is known in a DNS lookup");
}

}
}
catch(UnknownHostException e1)
{
e1.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println(connections);

intToIp

public String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
((i >> 24) & 0xFF);
}

关于android - 如何在android中获取连接到同一个Wi-Fi网络的系统的所有IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035636/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com