Tutorial :Simple Sockets in Objective-C



Question:

I've had trouble finding simple guides/examples for basic sockets in Objective-C (using NSSocketPort with NSFileHandle or using CFSocket/CSNetwork). Can anyone recommend a guide or a useful example? I would appreciate this greatly! I have tried to use this, but it is incomplete. Thanks!

P.S. I have been stuck in this part of my project for awhile and am starting to get desperate for some help.


Solution:1

The easiest way I've found to do this is the Cocoa Async Socket class:

https://github.com/robbiehanson/CocoaAsyncSocket

It's pretty straight forward to use, there's good html documentation included.


Solution:2

I know I am submitting answer to an very old question. In case you(visitor/stackoverflow {lover/users}) want to code your own asynchronous socket. All you need to do is detach native socket handle from connected CFSocketRef object.

void TCPClientCallBackHandler(CFSocketRef s, CFSocketCallBackType callbacktype,CFDataRef address, const void *data,void *info){        ClientSocket *obj_client_ptr=(__bridge ClientSocket*)info;      switch (callbacktype) {          case kCFSocketConnectCallBack :              if(data){                  [obj_client_ptr StopClient];              }              else{ //detach socket started                    CFSocketNativeHandle handle=CFSocketGetNative(s);                  CFSocketSetSocketFlags(s, 0);                  CFSocketInvalidate(s);                  CFRelease(s);                  s=nil;//detach socket ended                    [obj_client_ptr ConfigureStream:handle];                  // handle pass to CFStreamCreatePairWithSocket then bridge to NSStream              }              break;          }  }  

if still not got it, then watch it on youtube: https://www.youtube.com/watch?v=bJP4nysTmnI


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »