Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSData encoding issues...



On Tuesday, September 30, 2003, at 12:50 PM, Alex Lock wrote:

Hey all,

So I've got a socket problem. Actually it's not so much a socket problem as it is an NSData problem.

I'm working on an app where (unfortunately) the networking protocols are already written/being used...

All communication (in existing versions) is handled by directly writing characters to the socket, as in:

send (socketfd, &charbuf, length, 0);
recv (socketfd, &charbuf, length, 0);

Where the character buffers are regular (unterminated) C strings. For a new-line delimeter, it uses unsigned char 0xFF (int 255).

Here's the problem... I've tried OmniNetworking, SmallSockets, and now AsyncSockets (which I like best because it supports delegation exactly the way I would implement it myself); they all offer the same problem, text encoding.

Since all data sent to/received from the server in these classes is encapsulated inside an NSData object, I can't simply reference characters directly as I would if I was using the above (raw) socket functions. I've tried using:
[snipped]

It's probably freaking out because of the 255. How I'd handle this with AsyncSocket is to use readDataToData: to read one line at a time. Use an NSData of one byte, 255, as the ...ToData: parameter. Then use this:

int lineLength = [data length] - 1 /* to ignore the 255 */;
NSString *line = [NSString stringWithCString:[data bytes] length:lineLength];

That should work.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSData encoding issues... (From: Alex Lock <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.