2012-12-04 97 views
0

我有一個TCP流連接由一個發送聯繫人的巨大的NSDictionary,一個,從手機到服務器。如果NSDictionary有50個條目,它就可以,但發送大約150個155個聯繫人後,大約有200個應用程序崩潰。我認爲這可能是一個內存問題,或者流連接有一些限制?如果存在內存問題,就像我在ARC上一樣,我該如何解決它?IOS - TCP IP崩潰

響應處理(其中我認爲這可能是問題,特別是因爲流越來越關閉了很多次):

  • (無效)流:(NSStream *)theStream的handleEvent:(NSStreamEvent )streamEvent {

    NSLog(@「stream event%i」,streamEvent); recebeuResposta = YES; 開關(streamEvent){

    case NSStreamEventOpenCompleted: 
        NSLog(@"Stream opened"); 
        [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"serverResponseArrived" 
        object:nil]; 
        break; 
    case NSStreamEventHasBytesAvailable: 
    
        if (theStream == inputStream) { 
    
         uint8_t buffer[10240]; 
         int len; 
    
         while ([inputStream hasBytesAvailable]) { 
          len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
          if (len > 0) { 
    
           NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; 
            //NSLog(@"server said: %@", output); 
           NSArray *firstSplit = [output componentsSeparatedByString:@"=end="]; 
            // NSLog(@"firstSplit, %@",[firstSplit objectAtIndex:0]); 
           NSError *parseError = nil; 
           NSDictionary *outputDictionary =[[NSDictionary alloc]init]; 
           outputDictionary = 
           [NSJSONSerialization JSONObjectWithData: [[firstSplit objectAtIndex:0] dataUsingEncoding:NSASCIIStringEncoding] 
                   options: NSJSONReadingAllowFragments 
                    error: &parseError];       
            // NSLog(@"server said outputDictionary: %@", outputDictionary); 
    
           if (nil != output) { 
            if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"connect"]) 
            { 
              // NSLog(@"stream with server is opened. ready to send contacts."); 
              // NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]); 
             [[NSNotificationCenter defaultCenter] 
             postNotificationName:@"beginSyncronization" 
             object:nil]; 
            }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"add"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"done"]&&[(NSString*)[outputDictionary objectForKey:@"element"]isEqualToString:@"contact"]) 
            { 
              // NSLog(@"enviar data"); 
             [[NSNotificationCenter defaultCenter] 
             postNotificationName:@"sendNextContact" 
             object:nil]; 
              //[self prepareDetails]; 
            }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"add"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"error"]&&[(NSString*)[outputDictionary objectForKey:@"element"]isEqualToString:@"contact"]) 
            { 
              //  NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]); 
             [[NSNotificationCenter defaultCenter] 
             postNotificationName:@"sendSameContactTCP" 
             object:nil]; 
            }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"SyncMobile"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"error"]) 
            { 
              // NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]); 
             [[NSNotificationCenter defaultCenter] 
             postNotificationName:@"sendSameContactTCP" 
             object:nil]; 
            } 
           } 
          } 
         } 
        }else{ 
         NSLog(@"STREAM HAS NO BYTES! %@:",theStream); 
        } 
        break; 
    
    
    case NSStreamEventErrorOccurred: 
    
        NSLog(@"Can not connect to the host!"); 
        break; 
    
    case NSStreamEventEndEncountered: 
    
        NSLog(@"Stream closed"); 
        [theStream close]; 
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
        theStream = nil; 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendSameContactTCP" object:self]; 
        break; 
    default: 
        NSLog(@"Unknown event"); 
    /* [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"sendSameContactTCP" 
        object:nil];*/ 
    

    }

}

+1

你必須讓一些傳輸數據的代碼。我們無法猜測你可能寫錯了什麼。 –

+0

我使用代碼處理服務器的響應來更新問題。我認爲問題在於流何時關閉。我試圖在這一點上重新啓動流。 –

回答

0

我用從robbiehanson的CocoaAsyncSocket庫,而不是,並且它的速度更快和穩定:

https://github.com/robbiehanson/CocoaAsyncSocket

如果你必須做套接字nections我強烈建議使用它,它更快,更簡單(至少對我來說)。