Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Recieving 2 packets or more as 1

Recieving 2 packets or more as 1
Thread Tools
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Jul 24, 2003, 05:07 AM
 
Hey all,
In my app I'm using smallsockets v0.9 to communicate over a tcp/ip network. Though I've run into a problem.
I'm sending data using the socket function

- (void)writeDataNSData*)data

and I read the incomming data with the function

- (int)readDataNSMutableData*)data

Though for exampl if i send 2 pieces of data in one function straight after eachother the other end recieves these data items as 1 data item.

eg.

Code:
[socket writeData:myData]; [socket writeData:yourData];
;
-------
Code:
[socket readData:response]; NSLog(@"%@", response);
;

You would expect his to output something like

[TestApp]:: <1234 1234 1234>
[TestApp]:: <1234 1234 1234>

Though unfortunatly it outputs this:

[TestApp]:: <1234 1234 1234> <1234 1234 1234>

Any help would be appreciated
     
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Jul 24, 2003, 10:15 AM
 
Sup Syphor, I take it no one could help you with this problem @ spymac

Well I use some kind of spiffy threaded version of SmallSockets I coded, and, well I *had* (for 10 mins ) the same problem.

It's all in the packet structure.

For instance, I use this structure (notice the fixed lengths)

[__] [_] [_....................]

[packet length as unsigned short (2 bytes)] [packet type as char (1 byte)] [packet contents as char array (undefined amount of bytes, max would theorically be the max value of an unsigned short, which is 65,535).]

Also, I have an object, SGPacket and its subclass, (all the possible other types) SGResetPacket, SGChatPacket, etc.....

After receiving the data, I take the data and call the -packets message on it.

But wait, that's not in the regular NSData instance methods, you're right, I made one extension.

Code:
@interface NSData (NSData+SGPacketsAdditions) - (NSArray *)packets; // That returns an array of SGPackets contained in receiver's content @end
So I call -packets on it, and it returns an array of packets, and I treat the one by one with my handlePacket method.

But. How do I know how many packets there are?

It's easy, but you know that (given that the packet is not malformed) the first 2 bytes of the data you'll receive indicate the LENGTH of the first packet. So you calculate where it stops, and take those bytes, init a NSData with them, and pass them to your Packet object. Something like

Code:
+ (SGPacket *)packetWithData:(NSData *)data
for each packet.

Now, it is SGPacket's responsibility to analyze the data, of course, and return the right SGPacket subclass.

I'm not a network expert, but I think this is how it should act. Please, correct me if I'm wrong, I'm about to ship my app

PS: Where did you find SmallSockets 0.9???????
     
Syphor  (op)
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Jul 24, 2003, 05:03 PM
 
Wow, I would love to have that spiffy threaded version of smallsockets... if u can could u please email it to syphor@mac.com

P.S. I meant version 0.6
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:13 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2