作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个包含两个组件的应用程序,一个 iPhone 组件和一个 Mac 组件。他们应该通过 bonjour 相互交流。我在Mac端使用以下代码来查找服务的端口:
NSSocketPort *socket = [[NSSocketPort alloc] init];
struct sockaddr *addr = (struct sockaddr *)[[socket address] bytes];
int port = 9876;
if(addr->sa_family == AF_INET) {
port = ntohs(((struct sockaddr_in *)addr)->sin_port);
} else if(addr->sa_family == AF_INET6) {
port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
} else {
[socket release];
socket = nil;
NSLog(@"The family is neither IPv4 nor IPv6. Can't handle!!!");
}
我也可以在iPhone端使用这段代码并在模拟器中运行该应用程序,并且连接工作正常。然而,当我尝试在实际的 iPhone 上运行这段代码时,我发现 NSSocketPort 在 iPhone 上不可用。当我尝试使用端口 9876 启动服务时,Mac 应用程序在我尝试连接时显示连接被拒绝错误。那么如何在不使用 NSSocketPort 的情况下找到要使用的端口呢?
最佳答案
好吧,我查看了 Apple 的 WiTap 代码,并稍微修改了它,为自己编写了一个获取端口的方法:
- (int) getPort {
CFSocketContext socketCtxt = {0, self, NULL, NULL, NULL};
// Start by trying to do everything with IPv6. This will work for both IPv4 and IPv6 clients
// via the miracle of mapped IPv4 addresses.
CFSocketRef witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET6, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
uint32_t protocolFamily;
if (witap_socket != NULL) // the socket was created successfully
{
protocolFamily = PF_INET6;
} else // there was an error creating the IPv6 socket - could be running under iOS 3.x
{
witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
if (witap_socket != NULL)
{
protocolFamily = PF_INET;
}
}
/*if (NULL == witap_socket) {
if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerNoSocketsAvailable userInfo:nil];
if (witap_socket) CFRelease(witap_socket);
witap_socket = NULL;
return NO;
}*/
int yes = 1;
setsockopt(CFSocketGetNative(witap_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
// set up the IP endpoint; use port 0, so the kernel will choose an arbitrary port for us, which will be advertised using Bonjour
if (protocolFamily == PF_INET6)
{
struct sockaddr_in6 addr6;
memset(&addr6, 0, sizeof(addr6));
addr6.sin6_len = sizeof(addr6);
addr6.sin6_family = AF_INET6;
addr6.sin6_port = 0;
addr6.sin6_flowinfo = 0;
addr6.sin6_addr = in6addr_any;
NSData *address6 = [NSData dataWithBytes:&addr6 length:sizeof(addr6)];
CFSocketSetAddress(witap_socket, (CFDataRef)address6);
/*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address6)) {
if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv6Address userInfo:nil];
if (witap_socket) CFRelease(witap_socket);
witap_socket = NULL;
return NO;
}*/
// now that the binding was successful, we get the port number
// -- we will need it for the NSNetService
NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
memcpy(&addr6, [addr bytes], [addr length]);
return ntohs(addr6.sin6_port);
} else {
struct sockaddr_in addr4;
memset(&addr4, 0, sizeof(addr4));
addr4.sin_len = sizeof(addr4);
addr4.sin_family = AF_INET;
addr4.sin_port = 0;
addr4.sin_addr.s_addr = htonl(INADDR_ANY);
NSData *address4 = [NSData dataWithBytes:&addr4 length:sizeof(addr4)];
CFSocketSetAddress(witap_socket, (CFDataRef)address4);
/*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address4)) {
if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv4Address userInfo:nil];
if (witap_socket) CFRelease(witap_socket);
witap_socket = NULL;
return NO;
}*/
// now that the binding was successful, we get the port number
// -- we will need it for the NSNetService
NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
memcpy(&addr4, [addr bytes], [addr length]);
return ntohs(addr4.sin_port);
}
}
我删除了很多错误内容,因此它可能不是最佳的,但它有效。
关于iphone - 如何在 iOS 上找到 NSNetService 的正确端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11689830/
我是一名优秀的程序员,十分优秀!