iOS - 从服务器接收二进制数据仅在调试模式下工作

时间:2012-03-15 15:54:31

标签: iphone ios debugging nsstream

我有一个关于my last post

的问题

但我想起初我应该提供一些代码:

这是处理我创建的对象的函数

- (void)handleObject:(NSMutableData *)object {

NSLog(@"[object length] = %d", [object length]);
Byte *byteArray;
byteArray = (Byte *)[object bytes];

switch (byteArray[5]) {

    case 0: NSLog(@"Login OK"); 
        break;

    case 1: NSLog(@"Exit. Server disconnect");
        break;

    case 2: NSLog(@"Ping erhalten");
        break;

    case 4: NSLog(@"Points received");
        break;

    case 6: NSLog(@"transfer of POLYLINE vector objects");
        break;

    case 8: NSLog(@"transfer of POLYGON vector objects");
        break;

    case 9: NSLog(@"transfer of Render Rule");
        break;

    case 10: {

        NSLog(@"end of response for RequestVectorObjects");                                 
        isLoading = FALSE;
    }
        break;

    case 11: NSLog(@"Background Colorcode: %d", (byteArray[6] & 0xFF));
        break;    

    default: NSLog(@"From server: default value");
        break;
}

}

我在这里接收来自流的数据

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

switch(eventCode) {

    case NSStreamEventHasBytesAvailable:
    {
        isLoading = YES; 

        while (isLoading) {               

            uint8_t bufSize[4];            

            int packetLength = 0; 

            [(NSInputStream *)stream read:bufSize maxLength:4];
            packetLength = [self bytesToInt:bufSize withOffset:0];
            NSLog(@"packetLength: %d", packetLength);

            [tempStore appendBytes:bufSize length:4];

            if (packetLength == 25600) {

                loggedIn = YES;
                uint8_t buf[4];
                [(NSInputStream *)stream read:buf maxLength:4];
                [tempStore appendBytes:buf length:4];
                [self handleObject:tempStore];
            }
            else {

                uint8_t buf[packetLength];
                [(NSInputStream *)stream read:buf maxLength:packetLength];
                [tempStore appendBytes:buf length:packetLength];
                [self handleObject:tempStore];                    
            }
            [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
            [tempStore setLength:0];
        }           
        NSLog(@"Finished loading objects");                 

    } break;      

}

这一切都适用于模拟器或者如果我在iPhone上调试应用程序,但如果尝试在设备上运行它我总是得到BAD_EXCESS,因为packetLength的值是错误的所以我试着读得太多字节进入我的缓冲区。

现在我的问题:如何在调试模式下所有值都正确,但在运行模式下完全搞砸了?有什么我可以避免这个问题,或者我应该知道吗?

非常感谢您的帮助

这是我的BytesToInt函数,但如果收到的值正确,我认为它应该正常工作

- (int)bytesToInt:(Byte *)b withOffset:(int)offset {

int ret = 0;
for (int i = 0; i < 4; i++) {

    ret |= (b[offset + i] & 0xFF) << (3-i)*8;
}    
return ret;

}

2 个答案:

答案 0 :(得分:2)

我最后通过循环接收的数据直到达到maxLength值。解决起来并不难,但我表现得并不聪明......

嗯,我就是这样做的:

isLoading = YES; 

        while (isLoading) {               

            uint8_t bufSize[4];            

            int packetLength , maxLength, len;

            maxLength = 4;
            len = [(NSInputStream *)stream read:bufSize maxLength:maxLength];
            NSLog(@"len = %d", len);
            packetLength = [self bytesToInt:bufSize withOffset:0];
            NSLog(@"packetLength: %d", packetLength);

            [tempStore appendBytes:bufSize length:maxLength];

            if (packetLength == 25600) {

                loggedIn = YES;
                maxLength = 4;
                uint8_t buf[maxLength];                    
                len = [(NSInputStream *)stream read:buf maxLength:maxLength];
                NSLog(@"len = %d", len);
                [tempStore appendBytes:buf length:maxLength];
            }
            else {
                maxLength = packetLength;
                BOOL allDataReceived = NO;

                while (!allDataReceived) {

                    uint8_t buf[maxLength];                    
                    len = [(NSInputStream *)stream read:buf maxLength:maxLength];                                                                    
                    NSLog(@"len = %d", len);                       

                    if (len < maxLength) {

                        maxLength -= len;
                        [tempStore appendBytes:buf length:len];
                    }
                    else {

                        allDataReceived = YES;
                        [tempStore appendBytes:buf length:len];
                    }                            
                }                    
            }
            [self handleObject:tempStore];
            [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
            [tempStore setLength:0];
        }

答案 1 :(得分:1)

检查[NSInputStream read]的返回值。指定maxLength并不意味着实际上很多字节将真正读取。它可以更少,甚至可以为0.如果流只读取2个字节,我猜你将不得不发出第二个读数,指针指向已在缓冲区中读取的字节后面,并且其中包含剩余字节的maxLength缓冲液中。