Help needed on writing a TCP socket using NSStream in objective c
I current writing a TCP socket 开发者_如何学Cusing NSStream. I'm able to open the stream but i can't write out anything (eg. a NSString or NSData).
In your -stream:handleEvent:
delegate method, when you receive an NSStreamEventHasSpaceAvailable
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);
}
精彩评论