Re: Parsing a packet Async Socket
Re: Parsing a packet Async Socket
- Subject: Re: Parsing a packet Async Socket
- From: Andrea Salomoni <email@hidden>
- Date: Sun, 5 Mar 2006 10:35:42 +0100
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...
Any idea?
Thank you all
Andrea
On 04/mar/06, at 16:09, Agent M wrote:
Hi Andrea,
The -readData... methods return NSData objects (a byte stream) so
any interpretation is yours alone.
To parse the message you send asynchronously in the receiving
method, you should use an accumulation buffer (of object scope) to
first append the incoming data. This is because the whole message
may not have arrived at the time the message was posted. Once you
detect that the buffer contains a complete message, you can parse
it and post the event. Once the message is handled, remove the
bytes of the message from the FIFO buffer.
In your case below, you can use -bytes to get at the byte values
that interest you.
Good luck!
-M
On Mar 4, 2006, at 8:44 AM, Andrea Salomoni wrote:
Hi to all,
I'm trying to use Async Sockets to parse a packet returned from a
server.
My packet is divided into a packet header that contains a payload
length and a command.
How can I parse it?
I have something like this:
-(void)sendKeepAlive
{
char buff[20];
buff[0] = 17;
buff[1] = 0;
buff[2] = 10;
int i;
for (i=3; i<20; i++)
{
buff[i] = 0;
}
NSMutableData * keepAliveData = [[NSMutableData alloc]init];
[keepAliveData appendBytes:buff length:20];
[self writeOnServer:keepAliveData];
}
This is an header example .... If I use [socket
readDataWithTimeOut:-1 tag:1]; what kind of data is returned?
Thank you all for any kind of help
Andrea
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40themactionfaction.com
This email sent to email@hidden
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
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:
40metagraphics.it
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