CFStream:NSOutputStream无效

时间:2016-03-16 09:30:45

标签: ios sockets cfstream

我正在开发需要向Android Socket服务器发送消息的iOS应用程序。

我面临着一种奇怪的情况。如果我在NSOutputStream中连续写入数据,那么Android设备会获得Message,但是,如果我将代码写入While循环或任何If条件,那么它就无法工作。

 - (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);
    }
}

在上面的代码中,它连续写入NSOutputstream和Android设备获取数据。但是,如果我使用If条件取消注释,那么它将无效。 任何帮助将不胜感激。

0 个答案:

没有答案