iPhone:Bonjour NSNetService IP地址和端口

编程入门 行业动态 更新时间:2024-10-25 08:21:54
本文介绍了iPhone:Bonjour NSNetService IP地址和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请原谅我的iPhone / Objective-C新手状态!

Excuse my iPhone/Objective-C newbie status please!

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

I've found my HTTP server using NSNetServiceBrowser, but now I just want the IP address and port of the service found.

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

I've got something like the following in my delegate method:

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循环,即使调用了委托。有任何想法吗?谢谢!

but the for loop is never entered, even though the delegate is called. Any ideas? Thanks!

推荐答案

我意识到这是一个旧线程,但我也遇到了这个问题。上面的代码存在一些问题:

I realize this is an old thread, but I've just run across this as well. There are a few problems with the code above:

  • 这不是IPv6精明。最低,如果您的应用的其余只能处理v4 地址,它应该检测并且丢弃IPv6地址,但理想情况下你应该是准备好传递两个地址系列上游。

  • It's not IPv6 savvy. At a minimum, it should detect and discard IPv6 addresses if the rest of your app can only handle v4 addresses, but ideally you should be prepared to pass both address families upstream.

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

    The port assignment will generate incorrect values for Intel processors. You need to use htons to fix that.

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

    As Andrew noted above, the iteration should use the enhanced for loop.

    (编辑:添加此内容)如另一个相关主题所述,使用不鼓励 inet_ntoa 支持 inet_ntop 。

    ( Added this) As noted on another related thread, the use of inet_ntoa is discouraged in favor of inet_ntop.

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

    Putting all of this together, you get:

    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); } } }
  • 更多推荐

    iPhone:Bonjour NSNetService IP地址和端口

    本文发布于:2023-08-07 13:44:28,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1319372.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:端口   地址   Bonjour   iPhone   IP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!