can any one provide me a good way to send a NSArray object over wifi network. I have a code to sent a text message(string) over wifi network. my code is
Send
- (void) sendText:(NSString *)string {
const uint8_t *message = (const uint8_t *)[string UTF8String];
if (_outStream && [_outStream hasSpaceAvailable])
if([_outStream write:message maxLength:strlen((char *)message)] == -1)
NSLog(@"Failed sending data to peer"开发者_Python百科);
}
Receiver Side
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if (stream == _inStream) {
// read it in
unsigned int len = 0;
len = [_inStream read:buf maxLength:buffSize];
buf[len] = '\0';
if(!len) {
if ([stream streamStatus] != NSStreamStatusAtEnd)
NSLog(@"Failed reading data from peer");
} else {
NSString *message = [NSString stringWithUTF8String:(char *)buf]; }}
now the message contains my received message... However i need to send a NSArray object over wifi.can any one help me to do it..
You can use the NSKeyedArchiver
to get a NSData
object out of your NSArray
, the NSData
object is just a wrapper for binary data so you can send it over your socket. When you received the data, first transform it back into an NSData
object and then use the NSKeyedUnarchiver
class to get the array back.
You need mechanism (protocol) that will send elements of array from sender end and in the same way it will receive at the reception end
精彩评论