problem with kCFSocketReadCallBack

Posted by zp26 on Stack Overflow See other posts from Stack Overflow or by zp26
Published on 2010-04-25T21:49:01Z Indexed on 2010/04/25 21:53 UTC
Read the original article Hit count: 230

Hello. I have a problem with my program. I created a socket with "kCFSocketReadCallBack. My intention was to call the "acceptCallback" only when it receives a string to the socket. Instead my program does not just accept the connection always goes into "startReceive" stop doing so and sometimes crash the program. Can anybody help? Thanks

readSocket = CFSocketCreateWithNative(
                                                      NULL,
                                                      fd,
                                                      kCFSocketReadCallBack,
                                                      AcceptCallback,
                                                      &context
                                                      );


    static void AcceptCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
    // Called by CFSocket when someone connects to our listening socket.  
    // This implementation just bounces the request up to Objective-C.
    {

         ServerVistaController *  obj;

        #pragma unused(address)
        // assert(address == NULL);
        assert(data != NULL);

        obj = (ServerVistaController *) info;
        assert(obj != nil);

        #pragma unused(s)
        assert(s == obj->listeningSocket);           

        if (type & kCFSocketAcceptCallBack){
            [obj acceptConnection:*(int *)data];
        }

        if (type & kCFSocketAcceptCallBack){
            [obj startReceive:*(int *)data];
        }       
    }



    -(void)startReceive:(int)fd
    {

        CFReadStreamRef readStream = NULL;
        CFIndex bytes;
        UInt8 buffer[MAXLENGTH];


        CFStreamCreatePairWithSocket(
                                     kCFAllocatorDefault,
                                     fd,
                                     &readStream,
                                     NULL);
        if(!readStream){
            close(fd);
            [self   updateLabel:@"No readStream"];
        }

        CFReadStreamOpen(readStream);

        [self   updateLabel:@"OpenStream"];

        bytes = CFReadStreamRead(
                                     readStream,
                                     buffer,
                                     sizeof(buffer));

        if (bytes < 0) {
            [self   updateLabel:(NSString*)buffer];
            close(fd);
        }


        CFReadStreamClose(readStream);


    }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about networking