我正在使用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」添加到數據的末尾。
我的問題是怎麼回事爲什麼這個數據被添加到服務器的某個地方?謝謝你的幫助!
哇。這是一些低級別的東西。仔細查看Apple在InputStreams上的文檔。你可能會發現你錯過的東西。 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html – sangony
我不侮辱你。如果有什麼我付你讚美。低級別的意思是根,就像非常複雜。 Google「低級編程語言」。 – sangony
很抱歉,我認爲這是一種侮辱,相信它或不會發生在我這個網站不止一次。不幸的是,我仍然無法找到我在文檔中缺少的東西,但我會繼續挖掘互聯網。 – Charlie