iOS SimplePing代表 [英] iOS SimplePing delegates

查看:280
本文介绍了iOS SimplePing代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimplePing扫描我的LAN。
我试图对网络上的所有可用IP进行ping操作,因此我在第一个主机之前触发ping操作,然后代表再次为下一个主机触发ping操作。
这里的问题是,当主机不可访问时,将不会触发任何委托。
我正在使用以下代表:

I am using SimplePing to scan my LAN. I am trying to ping all the availables IPs on my network so I am firing the ping fore the first host and then the delegates are firing the ping again for the next host. The issue here is that when a host is unreachable no delegate is firing. I am using the following delegates:

- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address;
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error;
- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet;
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error;
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet;
 - (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet;

因此,如果成功,则 didReceivePingPacket

我试图减少ping超时以便使用,但似乎不起作用。
所以我的问题是:有什么方法可以知道使用SimplePing无法访问主机吗?

I tried to decrease the ping timeout in order to fire the didFailWithError delegate with this but it seems is not working. So my question is: Is there any way to know when a host is unreachable using SimplePing?

推荐答案

链接您提供的未设置 ping超时,而是设置了发送超时。恐怕在这种情况下不存在ping超时,因为网络操作已成功完成,您只是没有得到回复。

The link you provided doesn't set the "ping timeout", it sets the send timeout. A ping timeout doesn't exist in this context, I'm afraid, as the network operation was completed successfully, you just didn't get a response back. You'll have to check for this timeout condition yourself.

我要做的就是当您发送一个数据包时(在 didSendPacket中:方法),启动NSTimer。如果在计时器用尽之前执行了 didReceivePingResponsePacket:方法,则ping显然返回了,您可以使计时器无效;但是,如果您的计时器在收到响应数据包之前触发,则应假定发生 ping超时。

The way I would do it is when you send out a packet (in the didSendPacket: method), fire up a NSTimer. If the method didReceivePingResponsePacket: is executed before the timer runs out, the ping obviously did return and you can invalidate the timer; if however your timer fires before you receive a response packet, you should assume a "ping timeout" occurred.

在代码中,它看起来像这样(我正在写因此,...仔细检查语法,将变量和方法放在适当的位置,...代码应演示原理,但是):

In code it would look something like this (I'm writing this out of my head, so... double check syntax, place the variables and methods in appropriate places, ... the code should demonstrate the principle, though):

const PING_TIMEOUT = 1.0f
pingTimer: NSTimer = nil;

- (void)pingNextHost {
    // set up the next host IP address, whatever needs to be done
    simplePing.sendPingWithData:(<some NSData>);
}

- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet {
    pingTimer = [NSTimer scheduledTimerWithTimeInterval:PING_TIMEOUT target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
}

- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet {
    NSLog(@"got response packet, host reachable");
    // Invalidate timer
    [pingTimer invalidate];
    // Move to next host
    [self pingNextHost];
}

- (void):timerFired(NSTimer *)timer {
    NSLog(@"ping timeout occurred, host not reachable");
    // Move to next host
    [self pingNextHost];
}

这篇关于iOS SimplePing代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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