[英]Converting [NSNetservice addresses] to ip address string
This question was already asked here: NSNetservice IP address 这里已经提出了这个问题: NSNet服务IP地址
and here: iPhone: Bonjour NSNetService IP address and port 在这里: iPhone:Bonjour NSNetService IP地址和端口
I've used both of those to get where I'm at now. 我已经用这两个来达到我现在的位置。 My problem is the following method I have doesn't quite work:
我的问题是以下方法我不太有用:
- (NSString *)getStringFromAddressData:(NSData *)dataIn {
struct sockaddr_in *socketAddress = nil;
NSString *ipString = nil;
socketAddress = (struct sockaddr_in *)[dataIn bytes];
ipString = [NSString stringWithFormat: @"%s",
inet_ntoa(socketAddress->sin_addr)]; ///problem here
return ipString;
}
This gives a build error saying both "Dereferencing pointer to incomplete type" and "Implicit declaration of function 'inet_ntoa'" on the same line. 这给出了一个构建错误,说明在同一行上“解除指向不完整类型的指针”和“隐式声明函数'inet_ntoa'”。
I've checked and I don't think I'm missing some type of framework to get inet_ntoa
to work. 我已经检查过,我不认为我错过了某种类型的框架来让
inet_ntoa
工作。 Any idea what I'm doing wrong? 知道我做错了什么吗? thanks.
谢谢。
If you're getting implicit declaration of inet_ntoa, you probably need 如果你得到inet_ntoa的隐式声明,你可能需要
#include <arpa/inet.h>
(You can man inet_ntoa to get this info and read more about that function) (您可以通过man inet_ntoa获取此信息并阅读有关该功能的更多信息)
FWIW I've posted an updated code snippet for handling NSNetService address resolution over at FWIW我发布了一个更新的代码片段,用于处理NSNetService地址解析
iPhone: Bonjour NSNetService IP address and port . iPhone:Bonjour NSNetService IP地址和端口 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.