CFStream : NSOutputStream not working

280 Views Asked by At

I am developing iOS app which required to send message to Android Socket server.

I am facing a weird situation. If I continuously write data in NSOutputStream then Android device get Message but, If I write code into While loop or any If condition then it is not working.

 - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;{
    switch (eventCode) {

        case NSStreamEventOpenCompleted:{
            NSLog(@"Stream opened");
        }
            break;

        case NSStreamEventHasBytesAvailable:
            NSLog(@"Stream has bytes available");

            break;

        case NSStreamEventErrorOccurred:
            NSLog(@"Can not connect to the host!");
            break;

        case NSStreamEventEndEncountered:
            NSLog(@"Stream closed");

            break;
        case NSStreamEventHasSpaceAvailable:{

//            if (sendCount < 100){ //NOT WORKING
                uint8_t *readBytes = (uint8_t *)[_data mutableBytes];
                readBytes += byteIndex; // instance variable to move pointer
                int data_len = [_data length];
                unsigned int len = ((data_len - byteIndex >= 1024) ?
                                    1024 : (data_len-byteIndex));
                uint8_t buf[len];
                (void)memcpy(buf, readBytes, len);
                len = [outputStream write:(const uint8_t *)buf maxLength:len];
                //            byteIndex += len;
                byteIndex = 0;
                sendCount++;
//            }
        }
            break;

        default:

            NSLog(@"Unknown event: %@ : %lu", aStream, (unsigned long)eventCode);
    }
}

In above code, it continuously write into NSOutputstream and Android device get data. But If I uncomment line with If condition then it will not work. Any help will be appreciated.

0

There are 0 best solutions below