2014-03-01 73 views
1

我正在使用NSOutputStream和NSInputStream的應用程序嘗試一下,看看我能做些什麼。我修改了This tutorial以允許我使用舊計算機作爲服務器!我遇到的問題是我想通過NSOutputStream發送字典,然後收到它。不幸的是,服務器似乎正在添加一個字節的數據,所以我無法成功解析數據。這裏是我的代碼....數據從服務器添加

//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我得到的「*的錯誤信息 - [NSKeyedUnarchiver initForReadingWithData:]:難以理解的存檔(0X62,0x70,0x6c,0×69(0x73)的,0x74 ,0x30,0x30)「。

在查看數據後,它看起來都是一樣的,除了來自服務器的數據的最後兩個值,它將「0a」添加到數據的末尾。

我的問題是怎麼回事爲什麼這個數據被添加到服務器的某個地方?謝謝你的幫助!

+0

哇。這是一些低級別的東西。仔細查看Apple在InputStreams上的文檔。你可能會發現你錯過的東西。 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html – sangony

+0

我不侮辱你。如果有什麼我付你讚美。低級別的意思是根,就像非常複雜。 Google「低級編程語言」。 – sangony

+0

很抱歉,我認爲這是一種侮辱,相信它或不會發生在我這個網站不止一次。不幸的是,我仍然無法找到我在文檔中缺少的東西,但我會繼續挖掘互聯網。 – Charlie

回答

1

雖然我不知道什麼是實際出錯,以及如何使這工作只是發送數據和接收數據。你需要某種終止部分來完成這項工作,所以我使用了兩種方法將converint NSData轉換爲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); 
    } 

我不可能達到沒有sangony鏈接這一結論這個其他職位GCDAsyncSocket alters data while transporting it謝謝!

相關問題