NSOutputStream总是写入零字节

时间:2013-06-17 15:26:28

标签: ios nsstream cfnetwork

正好遵循Apple的文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Streams.html - 我无法让任何网络NSOutputStream 永远写入数据。它总是返回一个Apple的文档似乎不应该返回的数字(0 - Apple声称这是针对固定大小的流,而不是网络流?)。

所有Apple的示例代码都忽略了返回代码(似乎假设它是正面的 - 我在网上找到的很多样本都使用无符号类型!这是非常错误的:()。

我成功了:

  1. CFSocketCreate(),带有kCFSocketAcceptCallBack和指向handleConnect函数的指针
  2. 带有“any”和手动选择端口的CFSocketSetAddress()
  3. CFRunLoopAddSource()开始侦听端口
  4. ...所有这些都有效,通过远程登录到IP地址+端口确认......


    ...但是,只要遵循Apple文档,就会出现可怕的错误:

    void handleConnect (
                     CFSocketRef s,
                     CFSocketCallBackType callbackType,
                     CFDataRef address,
                     const void *data,
                     void *info
                     )
    {
        NSLog(@"handleConnect called with callbackType = %li", callbackType);
    
        (myclass) server = (myclass*) info;
    
        BOOL canAccept = callbackType & kCFSocketAcceptCallBack;
        BOOL canWrite = callbackType & kCFSocketWriteCallBack;
        BOOL canRead = callbackType & kCFSocketReadCallBack;
        NSLog(@" ... acceptable? %@  .. readable? %@ .. writeable? %@", canAccept?@"yes":@"no", canRead?@"yes":@"no", canWrite?@"yes":@"no" );
    
        if( canAccept)
        {
            NSLog(@"[%@] Accepted a socket connection from remote host. Address = %@", [server class], addressString );
            /**
             "which means that a new connection has been accepted. In this case, the data parameter of the callback is a pointer to a CFSocketNativeHandle value (an integer socket number) representing the socket.
    
             To handle the new incoming connections, you can use the CFStream, NSStream, or CFSocket APIs. The stream-based APIs are strongly recommended."
             */
    
            // "1. Create read and write streams for the socket with the CFStreamCreatePairWithSocket function."
            CFReadStreamRef clientInput = NULL;
            CFWriteStreamRef clientOutput = NULL;
    
            // for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
            CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
    
            CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &clientInput, &clientOutput);
    
            // "2. Cast the streams to an NSInputStream object and an NSOutputStream object if you are working in Cocoa."
    
            // "3. Use the streams as described in “Writing a TCP-Based Client.”
            self.outputBuffer = [NSMutableData dataWithData:[@"Hello" dataWithEncoding:NSUTF8StringEncoding]];
            self.outputBytesWrittenRecently = 0;
            ((NSOutputStream*)output).delegate = self;
    
            [((NSOutputStream*)output) scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [((NSOutputStream*)output) open]; // MUST go last, undocumented Apple bug
        }
    }
    

    ...和委托方法:

    -(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
    {
        BOOL streamOpened = eventCode & NSStreamEventOpenCompleted;
        BOOL streamReadyToWrite = eventCode & NSStreamEventHasSpaceAvailable;
        BOOL streamReadyToRead = eventCode & NSStreamEventHasBytesAvailable;
        BOOL streamEnded = eventCode & NSStreamEventEndEncountered;
        BOOL streamErrored = eventCode & NSStreamEventErrorOccurred;
    
        if( streamReadyToWrite )
    #define C_BUFFER_IN_MEMORY_SIZE_WRITE ( 4096 ) // Apple vaguely recommends 4k, but I tried numbers down to 100 with no effect
        uint8_t extraCBufferNSStreamSucks[ C_BUFFER_IN_MEMORY_SIZE_WRITE ];
        NSRange rangeOfBytesToAttemptToWriteNext = { self.outputBytesWrittenRecently, MIN( C_BUFFER_IN_MEMORY_SIZE_WRITE, self.outputBuffer.length - self.outputBytesWrittenRecently) };
        [self.outputBuffer getBytes:&extraCBufferNSStreamSucks range:rangeOfBytesToAttemptToWriteNext];
    
        NSLog(@"About to write data to stream, this might block...");
        NSInteger amountWritten = [self.outputS write:extraCBufferNSStreamSucks maxLength:C_BUFFER_IN_MEMORY_SIZE_WRITE];
        NSLog(@"...finished write data to stream, it might have blocked");
        if( amountWritten > 0 )
        {
            self.outputBytesWrittenRecently += amountWritten;
    
            NSRange totalWrittenRange = { 0, self.outputBytesWrittenRecently };
            NSLog(@"Written %i bytes: %@", amountWritten, [[[NSString alloc] initWithData:[self.outputBuffer subdataWithRange:totalWrittenRange] encoding:NSUTF8StringEncoding] autorelease] );
    
            if( self.outputBytesWrittenRecently == self.outputBuffer.length )
            {
                NSLog(@"Finished writing data!");
    
            }
        }
        else
            NSLog(@"ERROR: failed to write bytes to stream (bytes written = %i)", amountWritten);
    }
    

1 个答案:

答案 0 :(得分:1)

啊!未记载,但是......如果您的NSStream实例变为nil,而不是返回错误,Apple会返回成功 - 但是然后返回0表示写入的字节数。

就我而言,init方法中的拼写错误意味着我用nil覆盖了新捕获的NSOutputStream和NSInputStream。

哎呀。