Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS-从服务器接收二进制数据只能在调试模式下工作_Iphone_Ios_Debugging_Nsstream - Fatal编程技术网

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

Iphone iOS-从服务器接收二进制数据只能在调试模式下工作,iphone,ios,debugging,nsstream,Iphone,Ios,Debugging,Nsstream,我有一个关于你的问题 但我认为首先我应该提供一些代码: 这是处理我创建的对象的函数 - (void)handleObject:(NSMutableData *)object { NSLog(@"[object length] = %d", [object length]); Byte *byteArray; byteArray = (Byte *)[object bytes]; switch (byteArray[5]) { case 0: NSLog(@"Login OK");

我有一个关于你的问题

但我认为首先我应该提供一些代码:

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

- (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上调试应用程序,一切都很好,但是如果尝试在设备上运行它,我总是会出现错误,因为packetLength的值是错误的,所以我尝试在缓冲区中读取太多字节

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

非常感谢你的帮助

这是我的ByTestPoint函数,但我认为如果接收到的值正确,它会正常工作

- (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;
-(int)bytestpoint:(字节*)b带偏移量:(int)偏移量{
int-ret=0;
对于(int i=0;i<4;i++){

ret |=(b[offset+i]&0xFF)检查
[NSInputStream read]的返回值
。指定maxLength并不意味着实际上要读取很多字节。它可以更少,甚至可以是0。如果流只读取2个字节,我想您将不得不发出第二次读取,指针指向已在缓冲区中读取的字节之后,以及缓冲区中剩余字节的maxLength。

请检查是否重新读取将
[NSInputStream read]的值
。指定maxLength并不意味着实际上要读取很多字节。它可以更少,甚至可以是0。如果流只读取2个字节,我想您将不得不使用指针进行第二次读取,该指针指向已在缓冲区中读取的字节的后面,并使用缓冲区中剩余字节的maxLength。

通过循环接收到的数据直到我达到我的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];
        }
isLoading=YES;
当(孤岛加载){
uint8_t bufSize[4];
int packetLength,maxLength,len;
最大长度=4;
len=[(NSInputStream*)流读取:bufSize maxLength:maxLength];
NSLog(@“len=%d”,len);
packetLength=[self-bytestpoint:bufsizewithoffset:0];
NSLog(@“packetLength:%d”,packetLength);
[tempStore appendBytes:bufSize length:maxLength];
如果(包装长度=25600){
loggedIn=是;
最大长度=4;
uint8_t buf[最大长度];
len=[(NSInputStream*)流读取:buf maxLength:maxLength];
NSLog(@“len=%d”,len);
[tempStore appendBytes:buf长度:maxLength];
}
否则{
maxLength=包装长度;
BOOL allDataReceived=否;
而(!allDataReceived){
uint8_t buf[最大长度];
len=[(NSInputStream*)流读取:buf maxLength:maxLength];
NSLog(@“len=%d”,len);
if(长度<最大长度){
maxLength-=len;
[tempStore appendBytes:buf长度:len];
}
否则{
allDataReceived=是;
[tempStore appendBytes:buf长度:len];
}                            
}                    
}
[自处理对象:tempStore];
[tempStore resetBytesInRange:NSMakeRange(0,[tempStore长度]);
[临时存储设置长度:0];
}

我最终通过循环接收到的数据,直到达到我的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];
        }
isLoading=YES;
当(孤岛加载){
uint8_t bufSize[4];
int packetLength,maxLength,len;
最大长度=4;
len=[(NSInputStream*)流读取:bufSize maxLength:maxLength];
NSLog(@“len=%d”,len);
packetLength=[self-bytestpoint:bufsizewithoffset:0];
NSLog(@“packetLength:%d”,packetLength);
[tempStore appendBytes:bufSize length:maxLength];
如果(包装长度=25600){
loggedIn=是;
最大长度=4;
uint8_t buf[最大长度];
len=[(NSInputStream*)流读取:buf maxLength:maxLength];
NSLog(@“len=%d”,len);
[tempStore appendBytes:buf长度:maxLength];
}
否则{
maxLength=包装长度;
BOOL allDataReceived=否;
而(!allDataReceived){
uint8_t buf[最大长度];
len=[(NSInputStream*)流读取:buf maxLength:maxLength];
NSLog(@“len=%d”,len);
if(长度<最大长度){
maxLength-=len;
[tempStore appendBytes:buf长度:len];
}
否则{
allDataReceived=是;
[tempStore appendBytes:buf长度:len];
}                            
}                    
}
[自处理对象:tempStore];
[tempStore resetBytesInRange:NSMakeRange(0,[tempStore长度]);
[临时存储设置长度:0];
}

感谢您的回复。在返回值[NSInputStrea]之前,是否可以在while循环中读取数据