Ping一个IP地址或主机从.Net MAUI应用程序

huangapple go评论63阅读模式
英文:

Ping an IP address or a host from .Net MAUI App

问题

我已创建了一个全新的 .net MAUI 应用程序(VS 2022 Community)。

我正在尝试从我的 MainPage 中ping一个IP地址。

我的代码:

IPAddress ipAddress = IPAddress.Parse(hostOrIp);

PingReply reply = await ping.SendPingAsync(ipAddress);
if (reply.Status == IPStatus.Success)
{
	return true;
}
else
{
	return false;
}

然而,上面的代码似乎根本不起作用,无论是在Android模拟器上还是在实际的Android设备上都不起作用。所需的权限已经处理好了。每次我都会收到一个"System.Net.NetworkInformation.IPStatus.TimedOut"的回复,即使是已知的IP地址如8.8.8.8。

可能的问题是什么?

英文:

I have created a brand new .net MAUI App (VS 2022 Community).

I am trying to ping an IP address from my MainPage.

My code:

IPAddress ipAddress = IPAddress.Parse(hostOrIp);

PingReply reply = await ping.SendPingAsync(ipAddress);
if (reply.Status == IPStatus.Success)
{
	return true;
}
else
{
	return false;
}

However, the code above does not seem to work at all, not on Android emulator not on a physical Android device either. The requisite permissions are already taken care of. I am getting a "System.Net.NetworkInformation.IPStatus.TimedOut" reply every time even to well known IP addresses like 8.8.8.8.

What could be the problem?

答案1

得分: 1

I ran the program on emulator using any other IP, it will be TimedOut as you said:

private async void Button_Clicked(object sender, EventArgs e)
{
    try
    {
        Ping pingSender = new Ping();
        PingReply reply = await pingSender.SendPingAsync("47.75.18.65");
        if (reply.Status == IPStatus.Success)
        {
            //...
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

However, if I test it on a real device, it will be successful.

There is another way to achieve it when running the program on an emulator. You can make the ping by a Java Runtime command:

private async void Button_Clicked(object sender, EventArgs e)
{
#if ANDROID
    Java.Lang.Process p1 = Java.Lang.Runtime.GetRuntime().Exec("ping -c 1 47.75.18.65");
    int returnVal = p1.WaitFor();
#endif
}

returnVal is 1 indicating success, and here is the effect.

Wish these can help you.

英文:

I ran the program on emulator using any other IP, it will be TimedOut as you said:

private async void Button_Clicked(object sender, EventArgs e)
{
    try
    {
        Ping pingSender = new Ping();
        PingReply reply = await pingSender.SendPingAsync("47.75.18.65");
        if (reply.Status == IPStatus.Success)
        {
            //...
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Ping一个IP地址或主机从.Net MAUI应用程序

However, if I test it on real device, it will be successful.

There is another way to achieve it when run the program on emulator. You can make the ping by a Java Runtime command:

private async void Button_Clicked(object sender, EventArgs e)
{

#if ANDROID
        Java.Lang.Process p1 = Java.Lang.Runtime.GetRuntime().Exec("ping -c 1 47.75.18.65");
        int returnVal = p1.WaitFor();
#endif

}

returnVal is 1 indicating success, and here is the effect.

Wish these can help you.

huangapple
  • 本文由 发表于 2023年5月10日 17:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定