I current writing a TCP socket using NSStream. I'm able to open the stream but i can't write out anything (eg. a NSString or NSData).
+2
A:
In your -stream:handleEvent:
delegate method, when you receive an NSStreamEventHasBytesAvailable
event for the NSOutputStream
, you can send data using:
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);
}
Jeremy W. Sherman
2010-10-19 01:35:45
Hi, i have tried the code but i have got the error "error writing to stream <__NSCFOutputStream: 0x102009f0>: Error Domain=NSPOSIXErrorDomain Code=9 "The operation couldn’t be completed. Bad file descriptor" do anyone know the problem?
tan
2010-10-19 04:31:06
It sounds like it's not backed by a file descriptor. Did you open the stream? Are you trying to write from the delegate callback as described?
Jeremy W. Sherman
2010-10-19 04:50:03
Hi, i have used the following code to print out the streamStatus which returns a '2' so i guess the stream is open: NSLog(@"Status of outputStream: %i", [outputStream streamStatus]);
tan
2010-10-19 05:26:10