Re: Parsing a packet Async Socket
Re: Parsing a packet Async Socket
- Subject: Re: Parsing a packet Async Socket
- From: Erik Berends <email@hidden>
- Date: Mon, 6 Mar 2006 12:56:54 +0100
Hi Andrea,
This is a snipped of what I use in my code to parse a NSData object:
(in this case rcvdData)
short int frameType; // first two bytes
unsigned int serialNumber; // byte 10 to 14
NSRange serialRange;
serialRange.location = 10;
serialRange.length = 4;
[rcvdData getBytes:(void*)&frameType length:sizeof(short int)];
[rcvdData getBytes:(void*)&serialNumber range:serialRange];
Have a look in the documentation on the "getBytes:lenght:" method and
the "getBytes:range:" method from NSData.
ErikB
On Mar 6, 2006, at 10:28 AM, Andrea Salomoni wrote:
Hi and thks,
My packet is made in this way:
header 3 bytes
byte 1 and 2 = payload length
byte 3 = command
after= payload.
I need to read and separate the payload length and the command,
after it get the payload...
I know the total length of the data stream: [data length], but I
need the first two bytes .... and then the byte number 3....
How can I do?
Thank you all
Andrea
On 05/mar/06, at 17:19, Agent M wrote:
On Mar 5, 2006, at 4:35 AM, Andrea Salomoni wrote:
Hi & thks for answer,
The problem is that I don't know the method to parse a header and
a payload....
I tried something like this:
int contentLength = [data length];
unsigned char command[100];
NSMutableData * myReturnedData = [[NSMutableData alloc]init];
NSLog (@"data %@", [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]);
NSRange range = {2, 1};
[data getBytes:command range:range];
[myReturnedData appendBytes:command length:1];
In order to get only the command because my packet is made by the
payload length a command and the payload.
But this doesn't works...
First off, you need to check if the requisite number of bytes are
available. If not, just return and do nothing (wait until the
buffer is full of enough bytes to perform processing). Next, don't
print your data as a string- it will be useless because, from your
previous email, it was clear that you had embedded '\0's which
denotes the end of a string, so your NSLog method prints none of
the interesting data out.
Just use:
NSLog (@"data %@",data);
which will print out a hex representation.
Then, immediately concatenate the incoming buffer to the existing
accumulation buffer/FIFO. Then, remove and process as many
messages as you can from the data stream.
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
AgentM
email@hidden
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden