Re: Using NSFileHandleConnectionAcceptedNotification for a server process
Re: Using NSFileHandleConnectionAcceptedNotification for a server process
- Subject: Re: Using NSFileHandleConnectionAcceptedNotification for a server process
- From: Matt Mashyna <email@hidden>
- Date: Fri, 8 Feb 2008 20:14:28 -0500
On Feb 8, 2008, at 5:07 PM, Jens Alfke wrote:
On 8 Feb '08, at 1:56 PM, Matt Mashyna wrote:
I'm trying to build a simple server and I'm using NSFileHandle to
read the incoming requests. It works, more or less, but what I have
run into is strange. In my call back when I read from the file
handle I always only get the first 510 bytes.
The code you posted accepts new connections, but you didn't show the
code that reads data from a new connection. So I have to guess about
how it works ... one common gotcha is that -
readInBackgroundAndNotify: is a one-shot — it will only notify you
once. In that notification callback, you have to call it again to
register for the next bunch of data. If you don't re-register, the
effect is like what you're reporting.
—Jens
Well, I guess I was stuck on the idea that my problem was really that
I needed to increase the fd buffer so I would get the whole chunk of
data at one time but maybe I'm not thinking about it in the right way.
I can re-register for more data but then it's not clear to me how I
know that the client is done sending it's stream and waiting for a
reply. There doesn't seem to be a NSFileHandle method to see if more
data is waiting to be read. I know I'm being dense. I just need a slap!
Here's where it gets the connect notification:
- (void)newConnection:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSFileHandle *remoteFileHandle = [userInfo objectForKey:
NSFileHandleNotificationFileHandleItem];
NSNumber *errorNo = [userInfo objectForKey:@"NSFileHandleError"];
if( errorNo ) {
NSLog(@"NSFileHandle Error: %@", errorNo);
return;
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dataReceivedNotification:)
name:NSFileHandleReadCompletionNotification
object:remoteFileHandle];
[remoteFileHandle readInBackgroundAndNotify];
} // newConnection
And here's where I actually read it... 510 bytes or less, every time!
- (void)dataReceivedNotification:(NSNotification *)notification
{
NSData *data = [[notification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
NSString* foo = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
NSLog(foo);
[foo release];
// do stuff with the data now
} // dataReceivedNotification
_______________________________________________
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