请原谅我的 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 循环。有任何想法吗?谢谢!