Search code examples
cocoamacossocketsobjective-c++nsstream

Socket Programming in Mac / Cocoa


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


Solution

  • 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.