如何通过 NSOutputStream 发送整数?

发布于 2024-11-17 07:16:31 字数 1898 浏览 2 评论 0原文

因此,我正在制作一个通过 NSStream 不断发送点数组的 iOS 应用程序,但因为有时发送方会在接收方收到一个数组之前写入两个数组,所以我决定首先写入长度,然后写入数组数据本身,以便接收者知道要处理多少字节。

发送者:

if (_outStream && [_outStream hasSpaceAvailable]){
    const uint8_t *buffer = [data bytes];
    uint64_t length = CFSwapInt64HostToLittle([data length]);
    NSLog(@"Sending bytes with length: %llu", length);
    int er1 = [_outStream write:(const uint8_t *)&length maxLength:sizeof(length)];
    int er2 = [_outStream write:buffer maxLength:length];  
    if (er1 < 0 || er2 < 0) {
        [self _showAlert:@"Failed sending data to peer"];
    }
}

接收者:

case NSStreamEventHasBytesAvailable:
    {
        if (stream == _inStream) {
            uint8_t *b;
            int len = 0;
            len = [_inStream read:b maxLength:8];
            uint64_t dataLength = CFSwapInt64LittleToHost(*b);
            NSLog(@"Received bytes with length: %llu", dataLength);
            if(len < 0) {
                if ([stream streamStatus] != NSStreamStatusAtEnd)
                    [self _showAlert:@"Failed reading data from peer"];
            } else if (len > 0){
                uint8_t bytes[dataLength];
                int length = [_inStream read:bytes maxLength:dataLength];

                [currentDownload appendBytes:bytes length:length]; 
                 id pointsArray = [NSKeyedUnarchiver unarchiveObjectWithData:currentDownload];
                [currentDownload release];
                currentDownload = [[NSMutableData alloc] init];
                if ([pointsArray isKindOfClass:[NSArray class]]) {
                    [drawScene addNewPoint:[[pointsArray objectAtIndex:0] CGPointValue] previousPoint:[[pointsArray objectAtIndex:1] CGPointValue]];
                }

            }
        }
        break;
    }

问题是接收者接收到不正确的整数,因此它读取了不正确的字节数。

谁能帮我解决这个问题吗?

So I'm making an iOS app that constantly sends and array of points through NSStreams, but because sometimes the sender writes two arrays before the receiver receives one, I decided to first write the length, then the array data itself, so that the receiver knows how many bytes to process.

sender:

if (_outStream && [_outStream hasSpaceAvailable]){
    const uint8_t *buffer = [data bytes];
    uint64_t length = CFSwapInt64HostToLittle([data length]);
    NSLog(@"Sending bytes with length: %llu", length);
    int er1 = [_outStream write:(const uint8_t *)&length maxLength:sizeof(length)];
    int er2 = [_outStream write:buffer maxLength:length];  
    if (er1 < 0 || er2 < 0) {
        [self _showAlert:@"Failed sending data to peer"];
    }
}

receiver:

case NSStreamEventHasBytesAvailable:
    {
        if (stream == _inStream) {
            uint8_t *b;
            int len = 0;
            len = [_inStream read:b maxLength:8];
            uint64_t dataLength = CFSwapInt64LittleToHost(*b);
            NSLog(@"Received bytes with length: %llu", dataLength);
            if(len < 0) {
                if ([stream streamStatus] != NSStreamStatusAtEnd)
                    [self _showAlert:@"Failed reading data from peer"];
            } else if (len > 0){
                uint8_t bytes[dataLength];
                int length = [_inStream read:bytes maxLength:dataLength];

                [currentDownload appendBytes:bytes length:length]; 
                 id pointsArray = [NSKeyedUnarchiver unarchiveObjectWithData:currentDownload];
                [currentDownload release];
                currentDownload = [[NSMutableData alloc] init];
                if ([pointsArray isKindOfClass:[NSArray class]]) {
                    [drawScene addNewPoint:[[pointsArray objectAtIndex:0] CGPointValue] previousPoint:[[pointsArray objectAtIndex:1] CGPointValue]];
                }

            }
        }
        break;
    }

The problem is that the receiver receives an incorrect integer, hence it reads an incorrect amount of bytes.

Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2024-11-24 07:16:31

难道当您读取数据时,您是从旨在传达长度的介绍性位开始,而不是从实际数据开始?

对于:

[currentDownload appendBytes:bytes length:length];

尝试替换如下内容:

NSRange rangeLeftover = NSMakeRange(sizeof(uint8_t), [currentDownload length] - sizeof(uint8_t));
NSData *dataLeftover = [currentDownload subdataWithRange:rangeLeftover];
uint8_t bytesLeftover[[dataLeftover length]];
[dataLeftover getBytes:&bytesLeftover length:[dataLeftover length]];
currentDownload = [NSMutableData data]; // clear
[currentDownload appendBytes:bytesLeftover length:[dataLeftover length]];

Could it be that when you read the data, you're starting with the introductory bits intended to convey length, rather than starting with the actual data?

For:

[currentDownload appendBytes:bytes length:length];

try substituting something like this:

NSRange rangeLeftover = NSMakeRange(sizeof(uint8_t), [currentDownload length] - sizeof(uint8_t));
NSData *dataLeftover = [currentDownload subdataWithRange:rangeLeftover];
uint8_t bytesLeftover[[dataLeftover length]];
[dataLeftover getBytes:&bytesLeftover length:[dataLeftover length]];
currentDownload = [NSMutableData data]; // clear
[currentDownload appendBytes:bytesLeftover length:[dataLeftover length]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文