Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用Ping.send或sendsync始终成功,即使没有网络_C#_Android_Unity3d - Fatal编程技术网

c#使用Ping.send或sendsync始终成功,即使没有网络

c#使用Ping.send或sendsync始终成功,即使没有网络,c#,android,unity3d,C#,Android,Unity3d,我在一个问题上挣扎了很长时间。我正在使用unity,我想ping我的服务器和路由器。一开始我使用的是UnityPing类,它对大多数设备都适用,但是当我在Google Pixel(Android 7.1)上测试时,它总是返回-1。因此,我尝试使用System.Net.NetworkInformation ping我的服务器。这是我的密码: private void PingToServer() { AutoResetEvent waiter = new AutoResetEvent(fa

我在一个问题上挣扎了很长时间。我正在使用unity,我想ping我的服务器和路由器。一开始我使用的是UnityPing类,它对大多数设备都适用,但是当我在Google Pixel(Android 7.1)上测试时,它总是返回-1。因此,我尝试使用System.Net.NetworkInformation ping我的服务器。这是我的密码:

private void PingToServer()
{
    AutoResetEvent waiter = new AutoResetEvent(false);
    System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
    pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    int timeout = 10000;
    PingOptions options = new PingOptions(64, true);
    IPAddress address = IPAddress.Parse("119.81.194.57");
    pingSender.SendAsync(address, timeout, buffer, options, waiter);
}
private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        Console.WriteLine("Ping canceled.");
        ((AutoResetEvent)e.UserState).Set();
    }
    if (e.Error != null)
    {
        Console.WriteLine("Ping failed:");
        Console.WriteLine(e.Error.ToString());
        ((AutoResetEvent)e.UserState).Set();
    }
    PingReply reply = e.Reply;
    int pingTime = (int)reply.RoundtripTime;
    UnityEngine.Debug.Log(reply.RoundtripTime);
    ((AutoResetEvent)e.UserState).Set();
}
它总是返回一个RoundripTime,这似乎是一个有意义的数字,但当我尝试ping另一个无法访问甚至无法关闭internet的ip地址时,它总是返回一个带有reply.status等于IPStatus.Success的RoundripTime。现在我很困惑,我是否真的ping到了我的远程服务器


我确实检查了其他一些类似的问题,但没有解决问题。一些答案建议使用SendPingAsync而不是SendAsync,但这对于unity是不可能的。

我有一个按钮。单击我有此代码的事件。。。而且效果很好

    AutoResetEvent waiter = new AutoResetEvent(false);
    Ping pingSender = new Ping();
    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    int timeout = 100;
    PingOptions options = new PingOptions(64, true);
    IPAddress address = IPAddress.Parse("YourTestIP");
    PingReply reply = pingSender.Send(address, timeout, buffer, options);
    if (reply.Status == IPStatus.Success)
    {
        MessageBox.Show(reply.Address.ToString());
    }

尝试使用而不是
sendsync
this:
PingReply reply=pingSender.Send(地址、超时、缓冲区、选项)
并检查
如果(reply.Status==IPStatus.Success)
希望它有帮助。是的,我尝试了SendAsync()和Send(),正如我前面提到的,结果总是等于IPStatus。成功,即使没有internet连接。当我使用ping.Send()时,当我没有internet连接或IP错误时,它不会以成功告终。。但我认为,在这种情况下,它不会得到处理。给我一秒钟,我发布我的代码:)