从主机获取ping延迟 [英] Get ping latency from host

查看:174
本文介绍了从主机获取ping延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从主机获得延迟很长时间而且我陷入困境。已经尝试过简单Ping
,但似乎没有返回延迟。我最接近的是当我使用 TKC-PingTest 进行MAC OS时。这是完美的,但仅限于iPhone模拟器,因为当使用iPhone时,由于TKC使用的补丁/ sbin / ping,我收到错误。除了这两个,我已经尝试了很多其他的东西,什么都没有。

I'm trying to get the latency from host for a pretty good time and I'm stuck in. Already tried Simple Ping , but seems it doesn't return the latency. The closest I've done was when I use the TKC-PingTest for MAC OS. That works perfect but only in the iPhone Simulator because when use the iPhone I get an error due the patch "/sbin/ping" TKC uses. Besides these two, I already tried many others and got nothing.

推荐答案

你可以轻松扩展简单的ping来计算延迟。 Simpleping.h定义了SimplePingDelegate协议。有两种感兴趣的方法 - didSendPacket didReceivePingResponsePacket 。对延迟计时的简单实现将是

You can easily extend simple ping to calculate the latency. Simpleping.h defines the SimplePingDelegate protocol. There are two methods of interest - didSendPacket and didReceivePingResponsePacket. A naive implementation for timing the latency would be

@property (strong,nonatomic) NSDate *start;

- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
{
    self.start=[NSDate date];
}

- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
{
    NSDate *end=[NSDate date];
    double latency = [end timeIntervalSinceDate:self.start]*1000.0;

    //TODO - Do something with latency
}

我说这是一个niave实现,因为它不处理在收到响应之前发送另一个数据包或丢弃数据包的情况。为了解决这个问题,您需要检查数据包数据,以确定发送和接收事件之间的序列号是否一致。

I say this is a niave implementation because it doesn't deal with the case where another packet is sent before the response is received or where packets are dropped. To deal with this you would need to examine the packet data to determine whether the sequence number was consistent between the send and receive events.

这篇关于从主机获取ping延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆