NSNetService 工作正常,但无法获取主机名,解析导致错误

发布于 2024-11-17 12:35:12 字数 681 浏览 6 评论 0原文

我使用 Bonjour 和 NSNetService 从我的 iPhone 发布服务器。一切都按预期工作,我可以浏览我正在服务的页面等。但是,在 iPhone 上我想显示主机名(即 URL,如“myDevice.local”。),以便人们也可以输入在浏览器中手动输入地址(对于缺少 bonjour 发现服务的客户端很有用)。我的理解是,调用方法 [myNetService hostName] 应该给我该地址。然而,这个调用总是返回nil。我在一些论坛上读到,我首先应该解决该服务,但是 [myNetServiceresolve] 和 [myNetServiceresolveWithTimeout:10] 都调用了

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict;

带有错误的

{
    NSNetServicesErrorCode = -72003;
    NSNetServicesErrorDomain = 10;
}

委托方法,这显然意味着它已经解决了。同样,这一切都是在我可以使用该服务时发生的。我还可以获得端口、域和服务类型。唯一奇怪的事情是调用 [myNetService 地址] 返回一个空数组。

我使用的是 SDK 3.1.3。有人知道我可能做错了什么吗?

I'm using Bonjour with an NSNetService to publish a server from my iPhone. It all works as expected, I can browse the pages I'm serving etc. However, on the iPhone I want to display the host name (i.e. the URL, like "myDevice.local."), so that one can also enter the address manually in a browser (useful for clients missing a bonjour discovery service). My understanding is that calling the method [myNetService hostName] should give me that address. However, this call always returns nil. I read on some forum that I first should resolve the service, however both [myNetService resolve] and [myNetService resolveWithTimeout:10] call the delegate method

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict;

with the error

{
    NSNetServicesErrorCode = -72003;
    NSNetServicesErrorDomain = 10;
}

which apparently means that it is already resolved. Again, all this is happening while i can use the service. Also I can get the port, the domain, and the type of the service. The only other strange thing is that the call [myNetService addresses] returns an empty array.

I'm using SDK 3.1.3. Does anybody have an idea what I could be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬心 2024-11-24 12:35:12

[[NSProcessInfo processInfo] hostName] 方法对我来说没有用,原因有两个:

  • 如果蜂窝数据处于活动状态,它会在我的提供商的域中返回一些奇怪的主机名
  • 如果只有 WiFi,则主机名没问题,但全部小写

以下代码来自 http://iphoneappcode.blogspot.com/2012/05/how-to-get-device-ipaddess-in-iphone.html 最适合我,总是返回本地主机名敏感大小写:

+ (NSString *) hostname
{
    char baseHostName[256]; 
    int success = gethostname(baseHostName, 255);
    if (success != 0) return nil;
    baseHostName[255] = '\0';

#if !TARGET_IPHONE_SIMULATOR
    return [NSString stringWithFormat:@"%s.local", baseHostName];
#else
     return [NSString stringWithFormat:@"%s", baseHostName];
#endif
}

The [[NSProcessInfo processInfo] hostName] method was not useful for me for two reasons:

  • If celular data is active, it returns some weird hostname at my provider's domain
  • If there's only WiFi, the host name is ok, but all lower case

The following code from http://iphoneappcode.blogspot.com/2012/05/how-to-get-device-ipaddess-in-iphone.html worked best for me, always returning the local host name with sensitive case:

+ (NSString *) hostname
{
    char baseHostName[256]; 
    int success = gethostname(baseHostName, 255);
    if (success != 0) return nil;
    baseHostName[255] = '\0';

#if !TARGET_IPHONE_SIMULATOR
    return [NSString stringWithFormat:@"%s.local", baseHostName];
#else
     return [NSString stringWithFormat:@"%s", baseHostName];
#endif
}
小嗲 2024-11-24 12:35:12

您可以通过 [[NSProcessInfo processInfo] hostName] 获取主机名,而无需解析您刚刚发布的网络服务。

You can get the hostname via [[NSProcessInfo processInfo] hostName] without resolving the netservice you just published.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文