首页 > 解决方案 > 'PingCompletedEventArgs.reply' 为空

问题描述

是什么导致回复为空?我正在通过 Unity3D 构建游戏。

该代码在我的 PC 上运行 - ping 成功完成,我可以访问reply.RoundtripTime. 但是当在 iOS (iPhone) 上构建和运行时,回复仍然为空,即使_respose.Cancelled是假的并且Error是空的?手机已连接到互联网。

class MyPing{
    public SNNI.Ping ping{ get; private set;} = null;
    public Ip_Array ip{ get; private set;} = null;
    public SNNI.PingCompletedEventArgs _response{ get; private set;} = null;

    public bool _isDone{ 
        get{ return _response != null; }
    }

    public MyPing( Ip_Array ip, int ms_timeout=1000 ){
        this.ip = ip;
        ping = new SNNI.Ping();
        ping.PingCompleted += (object sender, SNNI.PingCompletedEventArgs r)=>{  _response = r; };

        System.Net.IPAddress addr = new System.Net.IPAddress( ip.ip );
        ping.SendAsync(addr, ms_timeout, null);
    }
}

.

//somewhere else:
p = new MyPing( new Ip_Array("0.0.0.0") );//<--actual ip instead of this placeholder

.

//later, checking every frame of the game:
if(p._isDone == false){  continue;  } 
if(p._response.Cancelled || p._response.Error != null){ continue; }
else{
    Debug.Log(p._response.reply.Status);//throws exception because .reply is somewhow 'null'
} 

有趣的是,有一个使用 PingCompletedEventArgs 的示例,特别是该DisplayReply()方法。他们在那里检查是否reply==null但从不解释为什么它可能为空......

标签: c#networking

解决方案


推荐阅读