import java.net.InetAddress;
public class PingExample {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("192.168.1.1");
if (address.isReachable(5000)) {
System.out.println("Host is reachable");
} else {
System.out.println("Host is NOT reachable");
}
}
}
| 在Windows上,可以使用以下C#代码示例:
using System.Net.NetworkInformation;
public class PingExample
{
public static void Main()
{
Ping ping = new Ping();
PingReply reply = ping.Send("192.168.1.1", 5000);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Host is reachable");
}
else
{
Console.WriteLine("Host is NOT reachable");
}
}
}
``` |
请注意,上述介绍中提到的Java代码示例适用于安卓应用开发,需要相应的网络权限才能运行,而C#代码是针对Windows平台的应用开发,在安卓设备上使用ping命令时,可能需要超级用户权限,因为某些终端模拟器可能没有完全的root访问权限。