我正在开发一个使用 NSOutputStream 和 NSInputStream 的应用程序来尝试一下,看看我能做什么。我已经修改了本教程以允许我使用旧计算机作为服务器!我遇到的问题是我试图通过 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”添加到数据的末尾。
我的问题是出了什么问题,为什么将这些数据添加到服务器的某个位置?谢谢你的帮助!