Having a weird error with NSStreams
Having a weird error with NSStreams
- Subject: Having a weird error with NSStreams
- From: Development <email@hidden>
- Date: Tue, 15 Sep 2009 14:36:40 -0700
Ok I have reused this code a million times and never had a problem
until now.
I'm attempting to send an NSDictionary from one iPhone to another.
Actually an iPod to the simulator. And one of two things happens.
Either the data is totally muxed Meaning sent completely out of order
OR it sends the data in order, but does it twice on the same
connection just out of phase with one another so the incoming data is
mixed up.
Below is my send and receive code: O and I got the bright idea to send
the NSDictionary after packaging it using NSKeyedArchiver but
unfortunately that results in crashes due to 'Incomprehensible
archive' errors.
- (void) sendData:(NSData*)dataToSend
{
if (_outStream) {
unsigned remainingToWrite = [dataToSend length];
void * marker = (void *)[dataToSend bytes];
while (0 < remainingToWrite) {
int actuallyWritten = 0;
actuallyWritten = [_outStream write:marker
maxLength:remainingToWrite];
remainingToWrite -= actuallyWritten;
marker += actuallyWritten;
}
}
}
case NSStreamEventHasBytesAvailable:
{
if (stream == _inStream) {
NSMutableData * dataBuffer = [[NSMutableData alloc]init/
*WithCapacity:(100*1024)*/];
if(!cardData) cardData = [[NSMutableData alloc]init];
uint8_t buf[16 * 1024];
uint8_t *buffer = NULL;
unsigned int len = 0;
while ((NSInputStream*)[stream hasBytesAvailable])
{
NSLog(@"In comming");
if (![(NSInputStream*)stream getBuffer:&buffer length:&len]) {
int amount = [(NSInputStream*)stream read:buf maxLength:sizeof
(buf)];
buffer = buf;
len = amount;
}
if (0 < len) {
if (!dataBuffer) {
dataBuffer = [[NSMutableData alloc] init];
}
[dataBuffer appendBytes:buffer length:len];
[cardData appendData:dataBuffer];
}
}
NSString * string = [[NSString alloc]initWithCString:[dataBuffer
bytes]];
if ([string rangeOfString:@"Ready"].length) {
[self sendCard];
}
else{
gotcard = YES;
[delegate decodeCard:cardData fromCommunicator:self];
[cardData release];
cardData = nil;
}
}
break;
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden