Obj-C SimplePing循环

时间:2015-02-15 09:43:27

标签: objective-c ping

Obj-C SimplePing Loop我试图遍历我的整个局域网并点击任何可能的IP以查看设备连接的位置,一种网络扫描仪,使用Apples SimplePing示例OS X我想出了如何发送Ping对于一个知识产权,但不知何故,当你试图循环这不起作用时它不会起作用,它只是第一次触发而不是停止...

在玩完它之后,我看到了2个Pings到前2个循环,但它总是停止...

我做错了什么?

到目前为止,我的代码看起来像这样简化:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    x = 0;
    foundDevices = [[NSMutableArray alloc] init];
    self.pinger = [[SimplePing alloc] init];
    [self startPinging];
}

-(void) startPinging{
    x = x +1;
    if(x < 256){
    NSString *hostName = [NSString stringWithFormat:@"192.168.1.%i",x];
    self.pinger = [SimplePing simplePingWithHostName:hostName];
    self.pinger.delegate = (id)self;
    [self.pinger start];
    }
}
- (void)sendPing{
    [self.pinger sendPingWithData:nil];
}
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address{
    [self sendPing];
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet{
    NSLog(@"%lu", (unsigned long)packet.length) ;
    [self.pinger stop];
    [self startPinging];
}

所以我只向每个IP发送一个Pind,等待回答和循环以防止同一个上的所有内容......

但这一切都不起作用......

Plz帮帮我

1 个答案:

答案 0 :(得分:1)

SimplePing计划在主runloop上,因此您需要在期望ping工作时运行循环。为此,在每次调用[self.pinger start]之后,插入与此类似的代码:

do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (self.pinger != nil);

收到你期望的最后一次ping后,将self.pinger设置为nil以打破do-while循环。 (您可以选择使用不同的标志来完成相同的循环中断,但这就是SimplePing项目中main.m文件的工作方式。)

相关问题