NSFileHandle throws funky exceptions when writing \n to socket
NSFileHandle throws funky exceptions when writing \n to socket
- Subject: NSFileHandle throws funky exceptions when writing \n to socket
- From: Tom Jakubowski <email@hidden>
- Date: Sun, 26 Jun 2005 05:54:29 -0500
I've written a (very basic) socket wrapper for my simple little
project, using a combination of classic BSD sockets and NSFileHandle
(per the macdevcenter.com article). I originally intended to use
ASyncSocket for my networking needs, but unfortunately it had some
unacceptable limitations (specifically, with reading chunks of data
from the socket that weren't newline-terminated without using up 90%
of the CPU). But, I digress.
When I try writing any string terminated with a "\n" to the socket, I
get a very funky exception thrown:
2005-06-26 05:41:45.216 ClassTester[9391] the last character of message is: 10
2005-06-26 05:41:45.216 ClassTester[9391] string: jaraim
2005-06-26 05:41:45.216 ClassTester[9391] writing <6a617261 696d0a>
2005-06-26 05:41:45.308 ClassTester[9391] Exception raised during
posting of notification. Ignored. exception: ***
-[NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to
insert nil value
Writing a string that's not terminated by "\n" apparently works fine,
but, unfortunately, for my purposes, all strings written to the socket
must be terminated with a newline.
Here are the relevant methods (along with some NSLog() calls for
debugging purposes):
The socket wrapper's writeStringToSocket method:
- (void)writeStringToSocket:(NSString *)message
{
NSData *messageData;
NSLog(@"the last character of message is: %d", [message
characterAtIndex:[message length]-1]);
messageData = [NSData dataWithBytes:[message UTF8String]
length:[message length]];
NSLog(@"string: %@", message);
NSLog(@"writing %@", messageData);
[fileHandle writeData:messageData];
}
And the action method in my controller class that's called when the
user wants to send some text (provided by NSTextField *inputField):
/* I've actually trimmed some lines that are irrelevant to actually
preparing the string and calling this method for readability */
- (IBAction)sendText:(id)sender
{
NSString *text;
text = [inputField stringValue];
text = [text stringByAppendingString:@"\n"];
[socket writeStringToSocket:text];
}
I did a search on this mailing list for problems pertaining to -
writeData: as well as exceptions thrown by NSPlaceholderDictionary,
but none of the results seemed related to my problems. I did notice,
though, that several other examples of code have written newlines to
NSFileHandle with no problems.
Thanks for the help.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden