2012-03-15 3 views
0

私はmy last postiOSの - サーバーからバイナリデータを受信すると、デバッグモードでのみ動作します

に関する質問があるしかし、私は最初に私はいくつかのコードを提供するべきだと思う:

これは私の作成したオブジェクト

を処理するための機能です
- (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;  

}

packetLengthの値が間違っているので、それはすべてのシミュレータまたは私はiPhone上でアプリケーションをデバッグする場合はうまく動作しますが、デバイス上でそれを実行しようとすると、私はいつもBAD_EXCESSを取得私はバッファにあまりにも多くのバイトを読み込もうとします。

私の質問に:どのようにすべての値がデバッグモードで正しいが、実行モードで完全に混乱することがありますか?その問題を避けるために何かできることはありますか、それとも気づくべきですか?

あなたのヘルプは非常に受け取った値が

- (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

私は最終的に、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]; 
     } 
+0

+1あなたのソリューションを投稿する – MrTJ

1

戻り値を確認し正しければ、ここで

は私BytesToInt機能があるが、私はそれが必要として、それがうまくいくと思う高く評価されあなたの[NSInputStream read]のmaxLengthを指定しても、実際には多くのバイトが、実際はになることはありません。ストリームが2バイトしか読んでいない場合は、すでにバッファに読み込まれているバイトの後ろを指すポインタで2番目の読み込みを実行する必要があり、残りのバイトのmaxLengthを指定する必要があります。バッファ。

+0

、あなたの 'bytesToInt' – MrTJ

+0

はありがとう私たちを見ますあなたの応答のために。 [NSInputStream read]の戻り値が自分のmaxLengthに達するまでwhileループのデータを読み込むことは可能ですか? – Bautzi89

+0

私はついにそれをしました。もう一度あなたの助けをありがとう – Bautzi89

関連する問題