开发者

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜