hassaanm
hassaanm

Reputation: 15629

Simple Sockets in Objective-C

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.

Upvotes: 2

Views: 8105

Answers (2)

Mininova Web
Mininova Web

Reputation: 75

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

Upvotes: 1

JosephH
JosephH

Reputation: 37505

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.

Upvotes: 0

Related Questions