• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Parsing a packet Async Socket
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing a packet Async Socket


  • Subject: Re: Parsing a packet Async Socket
  • From: Erik Berends <email@hidden>
  • Date: Mon, 6 Mar 2006 23:33:22 +0100

This is what helps for me:

NSLog(@"Frametype: %hi", frameType);
NSLog(@"Serialnumber: %u", serialNumber);

Have a look at: http://developer.apple.com/documentation/Cocoa/ Conceptual/DataFormatting/Articles/FormatStrings.html
(especially the "format specifiers")


Good luck ;-)

ErikB


On Mar 6, 2006, at 7:23 PM, Andrea Salomoni wrote:

Hi & thks

Yes I did already... the problem is to write in a NSLog the frame and serialNumber variables.


Thank you for support Andrea

On 06/mar/06, at 12:56, Erik Berends wrote:

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:
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
References: 
 >Parsing a packet Async Socket (From: Andrea Salomoni <email@hidden>)
 >Re: Parsing a packet Async Socket (From: Agent M <email@hidden>)
 >Re: Parsing a packet Async Socket (From: Andrea Salomoni <email@hidden>)
 >Re: Parsing a packet Async Socket (From: Andrea Salomoni <email@hidden>)
 >Re: Parsing a packet Async Socket (From: Erik Berends <email@hidden>)
 >Re: Parsing a packet Async Socket (From: Andrea Salomoni <email@hidden>)

  • Prev by Date: Re: Subclassed tableView no longer responding to draggingEntered
  • Next by Date: SQLite vs. Apple's Cocoa SQLite
  • Previous by thread: Re: Parsing a packet Async Socket
  • Next by thread: progress bar while creating a file
  • Index(es):
    • Date
    • Thread