2012-12-04 59 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我强烈建议使用它,它更快,更简单(至少对我来说)。