开发者

Help needed on writing a TCP socket using NSStream in objective c

开发者 https://www.devze.com 2023-01-20 13:33 出处:网络
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

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

精彩评论

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