2014-03-01 2 views
1

NSOutputStream 및 NSInputStream을 사용하는 앱에서이를 시도하고 내가 할 수있는 것을 확인하려고합니다. 서버로 오래된 컴퓨터를 사용할 수 있도록 This tutorial을 수정했습니다! 내가 겪고있는 문제는 NSOutputStream을 통해 사전을 보내려는 것입니다. 불행하게도 서버가 데이터 바이트를 추가하는 것 같아서 데이터를 성공적으로 보관 취소 할 수 없습니다. 이해할 수없는 아카이브 (0x62, 0x70, 0x6c, 0x69,에는 0x73, 따라 0x74 : [NSKeyedUnarchiver initForReadingWithData :] - 여기 내 코드 ....데이터가 서버에서 추가되었습니다.

//sending the data 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
     [dic setObject:@"hi" forKey:@"hello"]; 
     [dic setObject:@"whats up" forKey:@"huh"]; 
     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dic];//data is 365 bytes 
     NSLog(@"%@",data); 
     [self.outputStream write:data.bytes maxLength:data.length]; 



    //receiving data from NSInputStream 
    case NSStreamEventHasBytesAvailable: 
     NSLog(@"has bytes available"); 
     //_mutData = [NSMutableData new]; 
     _mutData = [[NSMutableData alloc] init]; 
     if (theStream == inputStream) { 

      uint8_t buffer[1024]; 
      int len; 

      while ([inputStream hasBytesAvailable]) { 
       len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
       if (len > 0) { 



        [_mutData appendBytes:buffer length:len]; 
        NSLog(@"Data: %@",_mutData); 
        NSLog(@"appended bytes"); 



       } 
      } 
     } 
     break; 

    // trying to make the data into the dictionary 
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:_mutData]; 
     NSLog(@"%@",dic); 

내가 * "의 오류 메시지가 unarchiveOjectWithData하려고시입니다 , 0x30, 0x30) ".

데이터를 보면 서버에서 들어오는 데이터의 마지막 두 값을 제외하고는 데이터 끝에 "0a"가 추가된다는 것을 제외하고는 모두 동일하게 보입니다.

내 질문에 왜이 데이터가 서버의 어딘가에 추가되는 이유가 무엇입니까? 어떤 도움을 주셔서 감사합니다!

+0

와우. 그것은 저급 수준입니다. InputStream에 관한 Apple의 문서를주의 깊게 살펴보십시오. 당신이 놓친 것을 발견 할 수도 있습니다. https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html – sangony

+0

나는 당신을 모욕하지 않습니다. 내가 너에게 칭찬을 지불했다면. 낮은 수준에서 나는 매우 복잡한 경우와 마찬가지로 루트를 의미했습니다. 구글 "낮은 수준의 프로그래밍 언어". – sangony

+0

죄송합니다. 믿을 수없는 모욕적 인 말은이 사이트에서 두 번 이상 나에게 일어났습니다. 불행히도 나는 여전히 내가 문서에서 누락 된 것을 발견 할 수는 없지만 인터넷에서 계속 파고들 것이다. – Charlie

답변

1

사실 실제로 어떤 일이 잘못되고 있는지 알지 못하고 어떻게 데이터를 보내고 데이터를 받는지 알 수 있습니다. NSString을 NSString으로 변환 한 다음 NSString을 NSData로 변환하는 두 가지 방법을 사용 했으므로 일종의 종료 부분이 필요합니다.

-(NSData *)interprateHexStringToData:(NSString *)hexString { 

    char const *chars = hexString.UTF8String; 
    NSUInteger charCount = strlen(chars); 
    NSUInteger byteCount = charCount/2; 
    uint8_t *bytes = malloc(byteCount); 
    for (int i = 0; i < byteCount; ++i) { 
    unsigned int value; 
    sscanf(chars + i * 2, "%2x", &value); 
    bytes[i] = value; 
    } 
    return [NSData dataWithBytesNoCopy:bytes length:byteCount freeWhenDone:YES]; 
    } 

    -(NSString *)makeDataIntoString:(NSData *)data { 

    NSUInteger dataLength = [data length]; 
    NSMutableString *string = [NSMutableString stringWithCapacity:dataLength*2]; 
    const unsigned char *dataBytes = [data bytes]; 
    for (NSInteger idx = 0; idx < dataLength; ++idx) { 
    [string appendFormat:@"%02x", dataBytes[idx]]; 
    } 

    return string; 

    } 

내 데이터를 전송하기 위해 나는이에 그것을 전환했다. 나는 다시 내가 이런 짓을 서버에서 그것을 가지고 한 번

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 

    [dic setObject:@"hi" forKey:@"hello"]; 
    [dic setObject:@"whats up" forKey:@"huh"]; 

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dic]; 

    NSLog(@"%@",data); 


    NSString *setUpString = [NSString stringWithFormat:@"||||%@",[self     makeDataIntoString:data]]; 
    NSData *stringData = [setUpString dataUsingEncoding:NSUTF8StringEncoding]; 



    [self.outputStream write:stringData.bytes maxLength:stringData.length]; 

내 데이터를 정렬하려면.

NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",stringData); 

    NSString *changed = [stringData stringByReplacingOccurrencesOfString:@"||||" withString:@""]; 
    NSLog(@"%@",changed); 
    NSData *dataFromTheString = [self interprateHexStringToData:changed]; 
    NSLog(@"%@",dataFromTheString); 
    @try { 
    NSDictionary *dictonary = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromTheString]; 
    NSLog(@"%@",dictonary); 
    } 
    @catch (NSException *exception) { 
    NSLog(@"%@",exception); 
    } 

나는이 다른 포스트 GCDAsyncSocket alters data while transporting it 덕분에 sangony 링크없이 이러한 결론에 도달 할 수 없습니다!