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.
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];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…