Reputation: 4467
If I use this to set up NSData then the writeData method crashes.
NSString *test = @"The quick brown fox jumped over the lazy dog\r\n";
NSData *data = [test dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:data withTimeout:10 tag:4];
However if I use this one then it works... but I need the NSString so I can enter a formatted string to send...
char bytes[] = "The quick brown fox jumped over the lazy dog\r\n";
NSData* data = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];
[asyncSocket writeData:data withTimeout:10 tag:4];
So what did I do wrong?
Upvotes: 1
Views: 1033
Reputation: 39
I know its bit late but I thought it would be good to know why this works - When you call writeData method with your data buffer, the asyncsocket code creates a new object by sublassing NSObject - AsyncWritePacket using the data passed down. It then uses retain and takes ownership of the data buffer you are passing hence why you dont want to release it.
@implementation AsyncWritePacket
- (id)initWithData:(NSData *)d timeout:(NSTimeInterval)t tag:(long)i
{
if((self = [super init]))
{
buffer = [d retain];
timeout = t;
tag = i;
bytesDone = 0;
}
return self;
@end
Upvotes: 0
Reputation: 4467
The NSString and the NSDate were not setup with alloc and init so they were gone when they got to the write data. I changed the NSDate to alloc and init and all works well now. These idea came from the several people that answered this. thanks for the help!
Upvotes: 1
Reputation: 15813
The latter is null terminated, the former is not. This is probably the issue.
Upvotes: 0