17

请原谅我的 iPhone/Objective-C 新手身份!

我使用 NSNetServiceBrowser 找到了我的 HTTP 服务器,但现在我只想找到服务的 IP 地址和端口。

我的委托方法中有以下内容:

NSNetService* server = [serverBrowser.servers objectAtIndex:0];

NSString            *name = nil;
NSData              *address = nil;
struct sockaddr_in  *socketAddress = nil;
NSString            *ipString = nil;
int                 port;
uint                 i;
for (i = 0; i < [[server addresses] count]; i++)
{
    name = [server name];
    address = [[server addresses] objectAtIndex:i];
    socketAddress = (struct sockaddr_in *)
    [address bytes];
    ipString = [NSString stringWithFormat: @"%s",
                inet_ntoa (socketAddress->sin_addr)];
    port = socketAddress->sin_port;
    NSLog(@"Server found is %s %d",ipString,port);
}

但是即使调用了委托,也永远不会进入 for 循环。有任何想法吗?谢谢!

4

3 回答 3

44

我意识到这是一个旧线程,但我也遇到过这个问题。上面的代码有几个问题:

  1. 它不是精通 IPv6 的。至少,如果您的应用程序的其余部分只能处理 v4 地址,它应该检测并丢弃 IPv6 地址,但理想情况下,您应该准备好将两个地址系列都传递给上游。

  2. 端口分配将为 Intel 处理器生成不正确的值。你需要用它htons 来解决这个问题。

  3. 正如 Andrew 上面提到的,迭代应该使用增强的 for 循环。

  4. (编辑:添加了这个)正如另一个相关线程所指出的,inet_ntoa不鼓励使用inet_ntop.

把所有这些放在一起,你会得到:

char addressBuffer[INET6_ADDRSTRLEN];

for (NSData *data in self.addresses)
{
    memset(addressBuffer, 0, INET6_ADDRSTRLEN);

    typedef union {
        struct sockaddr sa;
        struct sockaddr_in ipv4;
        struct sockaddr_in6 ipv6;
    } ip_socket_address;

    ip_socket_address *socketAddress = (ip_socket_address *)[data bytes];

    if (socketAddress && (socketAddress->sa.sa_family == AF_INET || socketAddress->sa.sa_family == AF_INET6))
    {
        const char *addressStr = inet_ntop(
                socketAddress->sa.sa_family,
                (socketAddress->sa.sa_family == AF_INET ? (void *)&(socketAddress->ipv4.sin_addr) : (void *)&(socketAddress->ipv6.sin6_addr)),
                addressBuffer,
                sizeof(addressBuffer));

        int port = ntohs(socketAddress->sa.sa_family == AF_INET ? socketAddress->ipv4.sin_port : socketAddress->ipv6.sin6_port);

        if (addressStr && port)
        {
            NSLog(@"Found service at %s:%d", addressStr, port);
        }
    }
}
于 2011-02-12T07:01:26.457 回答
18

您在回调中返回的 NSNetService 尚未准备好使用。您必须调用以下方法来获取它的地址:

- (void)resolveWithTimeout:(NSTimeInterval)timeout;

实现 NSNetService 委托方法以找出它何时解析:

- (void)netServiceDidResolveAddress:(NSNetService *)sender;

此时,服务中应该至少有一个地址。

另外,请注意仔细阅读文档和头文件!这里的问题有些复杂,我已经掩饰了。

于 2009-06-02T08:48:23.093 回答
3

在一个类别中重新混合接受的答案:

NSNetService+Util.h

#import <Foundation/Foundation.h>

@interface NSNetService (Util)

- (NSArray*) addressesAndPorts;

@end


@interface AddressAndPort : NSObject 

@property (nonatomic, assign) int port;
@property (nonatomic, strong)  NSString *address;

@end

NSNetService+Util.m

#import "NSNetService+Util.h"
#include <arpa/inet.h>

@implementation NSNetService (Util)

- (NSArray*) addressesAndPorts {

    // this came from http://stackoverflow.com/a/4976808/8047
    NSMutableArray *retVal = [NSMutableArray array];
    char addressBuffer[INET6_ADDRSTRLEN];

    for (NSData *data in self.addresses)
    {
        memset(addressBuffer, 0, INET6_ADDRSTRLEN);

        typedef union {
            struct sockaddr sa;
            struct sockaddr_in ipv4;
            struct sockaddr_in6 ipv6;
        } ip_socket_address;

        ip_socket_address *socketAddress = (ip_socket_address *)[data bytes];

        if (socketAddress && (socketAddress->sa.sa_family == AF_INET || socketAddress->sa.sa_family == AF_INET6))
        {
            const char *addressStr = inet_ntop(
                                               socketAddress->sa.sa_family,
                                               (socketAddress->sa.sa_family == AF_INET ? (void *)&(socketAddress->ipv4.sin_addr) : (void *)&(socketAddress->ipv6.sin6_addr)),
                                               addressBuffer,
                                               sizeof(addressBuffer));

            int port = ntohs(socketAddress->sa.sa_family == AF_INET ? socketAddress->ipv4.sin_port : socketAddress->ipv6.sin6_port);

            if (addressStr && port)
            {
                AddressAndPort *aAndP = [[AddressAndPort alloc] init];
                aAndP.address = [NSString stringWithCString:addressStr encoding:kCFStringEncodingUTF8];
                aAndP.port = port;
                [retVal addObject:aAndP];
            }
        }
    }
    return retVal;

}


@end


@implementation AddressAndPort
@end

[是的,我不怕创建很多NSObject实例……]

于 2016-09-19T16:02:52.420 回答