I need to sent NSData which holds JSON Strings as well total number of length in form of (length of actual string+actual string).I need to send a packet of data that reserves first 10 bytes for length of string and followed by string
while sending NSData object I also need to send its length in first 10 bytes followed by data like :
length of data + JSON string = total data sent to java client .
further java client will read first 10 byte to know actual length of data coming to make an byte array and move further.
This brute force example uses first 10 characters for string representation of payload length followed by actual payload.
NSArray *arrPayload = @[@"Hello", @"world"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arrPayload
options:0
error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSString *comboString = [NSString stringWithFormat:@"%010lu%@",
(unsigned long)jsonString.length, jsonString];
NSLog(@"%@", comboString);
NSData* combinedData = [comboString dataUsingEncoding:NSUTF8StringEncoding];
result:
0000000017["Hello","world"]
But: if this is supposed to be sent as a HTTP request you might want to consider using Content-Length header to pass the length information instead.