iOS上のNSnetServiceの適切なポートを見つける方法?
-
12-12-2019 - |
質問
私は2つのコンポーネント、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!!!");
}
.
i iPhoneの終わりでこのコードを使用してシミュレータでアプリを実行することもでき、接続は正常に動作します。ただし、実際のiPhoneでこのコードを実行しようとしたときは、NSSocketPortがiPhoneで利用できないことを検出しました。ポート9876でサービスを開始しようとすると、接続しようとするとMacアプリは接続拒否エラーを表示します。それでは、NSSocketPortを使用せずに使用するポートを見つけることができますか?
解決
OK私はアップルの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);
}
}
.
私はたぶん最適ではないので、たくさんのエラーのものを取り出しましたが、それは機能します。
他のヒント
iOS 7以降、NSNetServiceはソケットを作成してバインドできます。ポートゼロで初期化されている場合は、ランダムポートを選択します。
NSNetService *service = [[NSNetService alloc] initWithDomain:@"local." type:@"_test._tcp." name:@"Test Service" port:0];
[service publishWithOptions:NSNetServiceListenForConnections];
.
接続が確立されたときに、サービスはその代理人にnetService:didAcceptConnectionWithInputStream:outputStream:
を送信します。
所属していません StackOverflow