Mac / Cocoa 中的套接字编程
在我的应用程序中,我们需要与服务器进行套接字通信,通过谷歌搜索和Apple文档,发现了以下
1——NSStream类 2 -- CFSocket
我有点困惑,看起来 NSStream 是 CFSocket 类之上的包装器,而且,我需要安全套接字通信,并且我继续使用 NSStream / NSInputStream 和 NSOutput 流,
整个应用程序之前使用 OpenSSL现在它必须与 Native SSL 一起使用,任何人都可以帮助我指出正确的方向,
--我应该继续使用 NSStream 或 Cocoa 中可用的任何其他框架,
--如果 NSStream 在那里,它已经得到轮询并运行-循环哪个是最优选的,
--有什么例子可以参考,
提前致谢
In my application, we need to have socket communication with the server, and through googling and Apple documentation, came across following
1 -- NSStream Class
2 -- CFSocket
I am bit confused, it looks like NSStream is wrapper on top of CFSocket class, more over, i need to have Secure Socket Communication, and i went ahead using NSStream / NSInputStream and NSOutput stream,
Entire Application was previously using OpenSSL and now it has to go with Native SSL,can anyone help me to point correct direction,
-- Should i go ahead with NSStream or any other framework is available in Cocoa,
-- if NSStream is there, it has got poll and run-loop which is most preferred,
-- Is there any example available that i can refer,
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NSStream 可能是最好的解决方案,因为您不必关心每个细节。此外,运行循环方式更好,因为它检查是否有数据要读取,因此您不必这样做。
一般来说,最好使用可用的最高级别的抽象,并且仅在确实需要时才深入较低级别。网络编程有时很难正确且快速地完成,而 Cocoa 可以很好地为您处理这个问题。
请参阅 NSStream:TCP 和 SSL 博客文章,了解如何实现与 NSStream 建立 SSL 连接。虽然它针对的是 iOS,但在本例中它应该同样适用于 Mac OS X,因为我在博客文章中没有发现任何特定于 iOS 的代码或概念。
Using NSStream is probably the best solution as you don't have to care about every detail. Furthermore, the run-loop way is better as it checks whether there's data to read so you don't have to do it.
Generally, it's a good idea to use the highest level abstraction available and only dive lower-level if you really need to. Network programming can sometimes be tricky to do correctly and fast, and Cocoa does a good job of handling this for you.
See the NSStream: TCP and SSL blog post on how to establish an SSL connection with NSStream. While it aims at iOS, it should apply equally to Mac OS X in this case as I haven't spotted any iOS-specific code or concept in the blog post.