0

ネットワークのすべての IP アドレスに対して ping を実行しようとしています。ネットワーク内の他のすべての IP に ping を実行しようとして、IP を取得できるようになりました。私はこの IP xxx.xxx.xxx.121 を持っています。xxx.xxx.xxx.1 から xxx.xxx.xxx.255 に ping を実行する必要があります。ちなみに simpleping を使用しています。

for (int i = 0; i<256; i++) {
        NSString *str = [NSString stringWithFormat:@"%@.%i",searchingIp,i];
        NSLog(@"ipStr:%@",str);

        const char *address = [str UTF8String];
        NSLog(@"%s",address);

        struct sockaddr_in callAddress;
        callAddress.sin_len = sizeof(callAddress);
        callAddress.sin_family = AF_INET;
        callAddress.sin_port = htons(49160);
        callAddress.sin_addr.s_addr = inet_addr(address);

        NSLog(@"%s:%d",inet_ntoa(((struct sockaddr_in*)&callAddress)->sin_addr),((struct sockaddr_in*)&callAddress)->sin_port);


        simplePing = [SimplePing simplePingWithHostAddress:[NSData dataWithBytes:&callAddress length:sizeof(callAddress)]];
        simplePing.delegate = self;
        [simplePing start];
    }

また、「49160」でポートを設定しようとしましたが、それでも 2240 と表示されます。私はこのタイプのものではちょっと新しいです。

4

1 に答える 1

0

Apple が作成した SimplePing の例を使用しているとのことです ( https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html )

次に、ホスト名を使用して初期化できる方法があると思います:

+ (SimplePing *)simplePingWithHostName:(NSString *)hostName

そして、次のようなものを書く方が簡単なはずです:

for (int i = 0; i<256; i++) {
        NSString *str = [NSString stringWithFormat:@"%@.%i",searchingIp,i];
        NSLog(@"ipStr:%@",str);

        simplePing = [SimplePing simplePingWithHostName:str];
        simplePing.delegate = self;
        [simplePing start];
    }

ところで、ICMP は TCP/UDP ではないため、ポートの抽象化はないと思いますが、エコーのようなメッセージ タイプがあります。

于 2013-10-10T04:33:28.243 に答える