0

I am trying to send a dictionary of information from one iphone to another iphone through wifi/blutooth,for that i implement CFnetwork and NSNetservice Concept.I received Data in the Reciver side.

I did code as follows ....

**Sender Side**

 samDict=[NSMutableDictionary dictionary];
     [samDict setObject:@"Muhammed Musthafa" forKey:@"PP"];
     [samDict setObject:@"John P" forKey:@"Jose"];
     [samDict setObject:@"Lubaba" forKey:@"P"];
     [samDict setObject:@"JINI" forKey:@"KS"];
     [samDict setObject:@"Anusha" forKey:@"GS"];
     [samDict setObject:@"Athulya" forKey:@"V"];
     [samDict setObject:@"Riyas" forKey:@"MM"];
     [samDict setObject:@"Raihanath" forKey:@"MH"];
     [samDict setObject:@"Shabeer" forKey:@"poola"];
     [samDict setObject:@"Rajisha" forKey:@"Raji"];

     //NSLog(@" Dictionary values.............%@",samDict);

    NSMutableData *ArchiveData=[[NSMutableData alloc]init];    
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:ArchiveData];
    [archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];

    [archiver encodeObject:self.samDict forKey:@"Some Key Values"];
    [archiver finishEncoding];    
    [archiver release];

    const uint8_t *buffer=(const uint8_t *)[ArchiveData bytes];
    NSInteger nWrtitten=[_outStream write:buffer maxLength:[ArchiveData legth]];
    if (!nWrtitten)
    {
        NSLog(@"Eorror writting to Stream %@ : %@", _outStream, [_outStream streamError]);
    }
    else
    {

        NSLog(@" Write %ld bytes to Stream %@",(long)nWrtitten, _outStream);
    }

Receiver side

 NSInteger bytesRead;
        uint8_t buffer[13000];
        NSMutableData *data=[[NSMutableData data]retain];           

        if (stream==_inStream)
        {


            bytesRead=[_inStream read:buffer maxLength:sizeof(buffer)];
            if (bytesRead==-1&& bytesRead==0)
            {
                NSLog(@"_inStream Error...................");
            }
            else
            {                                       
                [data appendBytes:buffer length:bytesRead];
                NSLog(@"Data Received has length   :%d\n\n",[data length]);

                NSLog(@"data............%@",data);

            }
        }

I got data in Receiver Side.that is in bytes format . I can't UnArchive this data in Receiver side.if i am trying to unarchived this to Dictionary format like this

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
   NSMutableDictionary* UnArchiveDictionary =[NSMutableDictionary dictionary];
    UnArchiveDictionary = [[unarchiver decodeObjectForKey:@"Some Key Values"] retain]; 
    NSLog(@"UnArchive Dictionary  %@",UnArchiveDictionary);  

But unfortunately ican't Unarchived this data in Dictionary Format. i am getting Error of line NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

I have found some code for that converting bytes Data to String Format like this

size_t length=[data length];
                    unsigned char aBuffer[length];
                    [data getBytes:aBuffer length:length];
                    aBuffer[length - 1]=0;
                    NSString *messageString =aBuffer; 

                    NSLog (@"%s",messageString);

But my problem is how to get back the same dictionary . Why getting error in Unarchived time?

Musthafa P P
  • 645
  • 3
  • 7
  • 21
  • 1) What error do you get? 2) Please stick to naming conventions, don't start variables with uppercase letters. 3) Don't assign objects to variables if you overwrite them immediately (like your `UnArchiveDictionary`, the `[NSMutableDictionary dictionary]` is unnecessary (and the keyed unarchiver will give you an `NSDictionary`, not an `NSMutableDictionary`). – DarkDust Dec 20 '11 at 11:44
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x0, 0x0, 0x0, 0x0)' – Musthafa P P Dec 20 '11 at 11:50
  • if u don't mind can u explain NSKeyedUnarchiver incomprehensible archive exception – Musthafa P P Dec 20 '11 at 11:55

1 Answers1

0

The problem might be that the condition:

if (bytesRead==-1&& bytesRead==0)

will always be false. A variable can't have two different values at once. You probably want:

if (bytesRead==-1 || bytesRead==0)

So if there was an error (or no data was transmitted) your code wouldn't notice and instead append garbage bytes from the stack to your mutable data.

The keyed unarchiver then tries to interpret these garbage bytes as an archive and notices that it isn't, so it throws an exception.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • k,but i got data in receiver side .insted of NSMutableDictionary used NSDictionary then also the same error. what NSKeyedUnarchiver incomprehensible archive exception if don't mind can u explane – Musthafa P P Dec 20 '11 at 12:13
  • So, what value does `bytesRead` have? (I see that `[data length]` is logged, but I want to know the exact value in `bytesRead`). – DarkDust Dec 20 '11 at 12:19
  • the length of sending data and receiving data is same. i check both side i got same value – Musthafa P P Dec 20 '11 at 12:22
  • NSInteger bytesRead; uint8_t buffer[64]; NSMutableData *data=[[NSMutableData data]retain]; bytesRead=[_inStream read:buffer maxLength:sizeof(buffer)]; if (bytesRead==-1|| bytesRead==0) { NSLog(@"_inStream Error..................."); } else { [data appendBytes:buffer length:bytesRead]; NSLog(@"Data Received has length :%d\n\n",[data length]); NSString *text=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@" hi...................... %@",text); – Musthafa P P Dec 20 '11 at 12:28
  • i got same value, so my doubt is why again convert data to string NSString *text=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; – Musthafa P P Dec 20 '11 at 12:30