2010-10-19 83 views

回答

2

在你-stream:handleEvent:委託方法,當您收到NSStreamEventHasSpaceAvailable事件爲NSOutputStream,您可以使用發送數據:

NSData *data = /* get some data… */; 
const uint8_t *buffer = [data bytes]; 
NSUInteger length = [data length]; 
NSInteger nwritten = [outputStream write:buffer maxLength:length]; 
if (-1 == nwritten) { 
    NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]); 
} else { 
    NSLog(@"Wrote %ld bytes to stream %@.", (long)nwritten, outputStream); 
} 
+0

嗨,我已經嘗試過的代碼,但我有錯誤「錯誤寫入流<__ NSCFOutputStream:0x102009f0>:錯誤域= NSPOSIXErrorDomain代碼= 9「該操作不能完成。錯誤的文件描述符」做任何人都知道這個問題呢? – tan 2010-10-19 04:31:06

+0

這聽起來像它不是由一個文件描述符的支持。你有沒有打開流?你是想描述從委託回調寫? – 2010-10-19 04:50:03

+0

嗨,我已經使用了下面的代碼打印出它返回流狀態「2」,所以我猜流是開放的:\t的NSLog(@「OutputStream的狀態:%I」,[OutputStream的流狀態]); – tan 2010-10-19 05:26:10