ios - NSNetService 和 GCDAsynSocket 有什么区别?

标签 ios objective-c bonjour cocoaasyncsocket

在 iOS 上,我正在使用 bonjour 查找其他设备,以便我可以在两者之间传输数据。我计划将 NSNetService 用于 bonjour,将 CocoaAsyncSocket 用于流媒体。

this example ,他们在相同的端口上创建了一个GCDAsyncSocket和一个NSNetService:

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[socket acceptOnPort:0 error:NULL];

netService = [[NSNetService alloc] initWithDomain:@"local." type:@"_YourServiceName._tcp." name:@"" port:socket.localPort];

谁能解释一下 NSNetServiceGCDAsyncSocket 之间的区别?

对我来说,我似乎在同一个端口上创建了两个套接字。特别是因为您可以从 NSNetService 创建输入和输出流。

[service getInputStream:&input outputStream:&output];

最佳答案

GCDAsyncSocket 用于创建监听服务器套接字:

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[socket acceptOnPort:0 error:NULL];

端口号设置为 0,这意味着操作系统选择一个可用端口。

socket.localPort

是套接字随后监听的操作系统选择的端口号。

NSNetService 用于通过 Bonjour 发布服务:

netService = [[NSNetService alloc] initWithDomain:@"local." type:@"_YourServiceName._tcp." name:@"" port:socket.localPort];
[netService setDelegate:self];
[netService publish];

这不会创建另一个套接字,而是将创建的端口号与主机名和服务名一起使用,并在本地网络中发布此信息(使用 Bonjour/mDNS 协议(protocol))。

关于ios - NSNetService 和 GCDAsynSocket 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677395/

相关文章:

ios - 带有图像而不是标题的 UITableViewRowAction

android - Android NSD(网络服务发现)是否与 iOS 中的 Bonjour 服务兼容?

ios - AWS iOS SDK -> 缺少必需的架构 x86_64

ios - MPMoviePlayerController:停止 MPMoviePlayerController 时没有声音

ios - Cordova ios 图标(和闪屏)不显示 Ionic 资源

ios - NSBundle实例方法URLForResource :withExtension returning invalid CFStringRef on device

ios - 调用 alloc init 时 UIImagePickerController 真的很慢

c - 错误 C2016 : C requires that a struct or union has at least one member (while compiling Bonjour)

networking - 我可以从命令行使用 Bonjour 吗?

ios - 从另一个 View Controller 返回后如何为主 Storyboard保留大标题?