[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: NSRunLoop questions
From: |
Jay McCarthy |
Subject: |
Re: NSRunLoop questions |
Date: |
17 Dec 2001 14:01:29 -0500 |
So, I read up the NSFileHandle headers and source and started
experimenting with and I've hit a wall.
Currently my code is this (pardon my massive copy+paste, but my original
explanation that i wrote was basically the code so i figured it would be
better to have the code) -
#define NOTIFY(x,y) \
[[NSNotificationCenter defaultCenter] addObserver:self \
selector:(x) \
name: (y) object:nil];
listeningPort = [NSFileHandle fileHandleAsServerAtAddress: @"127.0.0.1"
service: @"9999" protocol: @"TCP"];
NOTIFY(@selector(newClient:),
NSFileHandleConnectionAcceptedNotification);
NOTIFY(@selector(clientInput:),
NSFileHandleReadCompletionNotification);
NOTIFY(@selector(clientInput:),
NSFileHandleDataAvailableNotification);
shouldRun = YES;
[listeningPort acceptConnectionInBackgroundAndNotify];
while(shouldRun) {
[NSRunLoop run];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
then in newClient -
NSDictionary *clientInfo = [notification userInfo];
NSFileHandle *clientHandle = [clientInfo objectForKey:
NSFileHandleNotificationFileHandleItem];
[listeningPort acceptConnectionInBackgroundAndNotify];
[clientHandle writeData: [NSData initWithString: @"Welcome\r\n"]];
[clientHandle waitForDataInBackgroundAndNotify];
and in clientInput -
NSDictionary *clientInfo = [notification userInfo];
NSFileHandle *clientHandle = [notification object];
if ( [notification name] == NSFileHandleReadCompletionNotification ) {
NSData *inputData = [clientInfo objectForKey:
NSFileHandleNotificationDataItem];
if ( [inputData length] > 0 ) {
[clientHandle writeData: inputData];
[clientHandle waitForDataInBackgroundAndNotify];
} else {
[clientHandle closeFile];
}
} else if ( [notification name] ==
NSFileHandleDataAvailableNotification ) {
[clientHandle readInBackgroundAndNotify];
}
**** END ****
The problem I'm having is that once I make two connections to
127.0.0.1:9999 - the second one is immediately disconnected :
~/GNUstep/Development/Projects $ telnet 127.0.0.1 9999
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Welcome
Connection closed by foreign host.
~/GNUstep/Development/Projects $
While the first one does just what I need. I tried to install
notification receievers for every notification that NSFileHandle gives
but none of them were called. I put into debugging to display what mode
of the RunLoop I was in at each step and it was always NSDefaultRunLoop.
So right now I have no idea what to do - it's probably something stupid
I'm not doing or something in the header/examples that i've overlooked.
TIA,
Jay
--
Jay McCarthy <jay@kenyamountain.com>