我正在将数据写入事件内部的流中:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode


如下所示(摘自Apple文档):

case NSStreamEventHasSpaceAvailable:
    uint8_t *readBytes = (uint8_t *)[self.outData mutableBytes];
    readBytes += byteIndex;
    int dataLength = [self.outData length];
    unsigned int length = ((dataLength - byteIndex >= 1024) ? 1024 : (dataLength - byteIndex));
    uint8_t buffer[length];
    (void)memcpy(buffer, readBytes, length);
    length = [self.outputStream write:(const uint8_t *)buffer maxLength:length];
    byteIndex += length;
    break;


但是,在输出流中没有其他可写内容时,将调用事件NSStreamEventEndEncountered,因此将丢弃输出流。如何保持此流开放供编写?根据Apple's documentation,此事件被正确调用,因为没有任何内容写入输出流。有任何想法吗?

最佳答案

您是否将流放入运行循环中?
请参见此处的示例:http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

关于objective-c - 如何在Objective-C中保持输出流打开?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11481077/

10-09 06:10