ios - 数据是从服务器添加的

标签 ios nsinputstream nsoutputstream

我正在开发一个使用 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:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)”。

在查看数据时,除了来自服务器的数据的最后两个值外,它似乎完全相同,它在数据末尾添加了“0a”。

我的问题是出了什么问题,为什么要将这些数据添加到服务器的某处?感谢您的帮助!

最佳答案

虽然我不知道到底出了什么问题,也不知道如何让它像发送数据和接收数据一样工作。您需要某种终止部分才能完成这项工作,因此我使用了两种将 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谢谢!

关于ios - 数据是从服务器添加的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22121824/

相关文章:

ios - 带上传流的 NSURLSession - 子类化 NSInputStream - com.apple.NSURLConnectionLoader 异常

ios - 使用 NSOutputStream 通过套接字发送到服务器时用换行符分隔数据(JSON)

ios - NSOutputStream 阻塞,没有触发 HasBytesAvailable 事件

cocoa - 使用 NSOutputStream POST 到 url

ios - 模糊效果 - 背景 UITextField

ios - 回合制多人 iOS 游戏(如 Words With Friends)的模板?

ios - 将 Web 服务从 GET 转换为 POST

iphone - 右栏按钮动画不正确

iphone - 从 iDevice 上传视频到 FTP

iOS NSStream 套接字读取 TCP 不稳定和截断