且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取本地网络上所有设备的名称和ip?

更新时间:2022-12-24 17:55:32

只需再次挖这个项目...这是我在@PRSingh请求时共享代码的尝试...希望能有所帮助.

Just dug up this project again...and this is my attempt at sharing the code as @PRSingh requested...hope it helps.

在我的.h中定义

 NSNetServiceBrowser *serviceBrowser;
 NSNetService *service;
 NSMutableArray* snames;
 NSMutableArray* sips;
 UIActivityIndicatorView* spinner;

在我的.m

#pragma mark - NSNetServiceDelegate Methods

- (void)netServiceWillResolve:(NSNetService *)sender {
    NSLog(@"netServiceWillResolve");

    serviceBrowser = [[NSNetServiceBrowser alloc] init];
    if(!serviceBrowser) {
        NSLog(@"The NSNetServiceBrowser couldn't be allocated and initialized.");
    }
    serviceBrowser.delegate = self;
    [serviceBrowser searchForServicesOfType:@"_type._tcp." inDomain:@"local."];
}

- (void)netServiceWillPublish:(NSNetService *)netService
{
    NSLog(@"netServiceWillPublish");
}

- (void)netServiceDidPublish:(NSNetService *)sender {
    NSLog(@"netServiceDidPublish");
}

- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict {
    NSLog(@"didNotPublish");
}

- (void)netServiceDidResolveAddress:(NSNetService *)netService {
    NSLog(@"netServiceDidResolveAddress");
    for (NSData* data in [netService addresses]) {

        char addressBuffer[100];

        struct sockaddr_in* socketAddress = (struct sockaddr_in*) [data bytes];

        int sockFamily = socketAddress->sin_family;

        if (sockFamily == AF_INET /*|| sockFamily == AF_INET6*/) {

            const char* addressStr = inet_ntop(sockFamily,
                                               &(socketAddress->sin_addr), addressBuffer,
                                               sizeof(addressBuffer));

           // int port = ntohs(socketAddress->sin_port);

            if (addressStr){
                if(![sips containsObject:[NSString stringWithFormat:@"%s",addressStr]]){
                    [snames addObject:netService.name];
                    [sips addObject:[NSString stringWithFormat:@"%s",addressStr]];
                    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
                }
            }

        }

    }
}

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
    NSLog(@"didNotResolve: %@",errorDict);
}

- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data {
    NSLog(@"didUpdateTXTRecordData");
}

- (void)netServiceDidStop:(NSNetService *)netService {
    NSLog(@"netServiceDidStop");
}

#pragma mark - NSNetServiceBrowserDelegate Methods

- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
    NSLog(@"didFindService: %@  lenght:%d",netService.name,[netService.name length]);
    [services addObject:netService];
}


- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
    [services removeObject:netService];
    [netService stop];
    int index = [snames indexOfObject:netService.name];
    [snames removeObjectAtIndex:index];
    [sips removeObjectAtIndex:index];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

    NSLog(@"didRemoveService");
}

要使其正常工作,我相信网络上的所有设备都必须运行"_type_.tcp"(或您给它的名字)的类型"的bonjour.您可以使用该类型,仅查看某些服务类型上您的本地网络上已经有哪些设备( https://developer.apple.com/library/mac/qa/qa1312/_index.html ).

For this to work I believe that all devices on the network need to be running bonjour with a 'type' of '_type_.tcp' (or what ever name you give it). You can play around with the type just to see what devices on your local network are already on certain service types (https://developer.apple.com/library/mac/qa/qa1312/_index.html).