I am _eventually_ writing an IRC obj-c/cocoa class that will make an
IRC connection and take care of housekeeping sort of things, but first
i am just trying to get a simple client working. I'm a little new to
objective-c so please bear with me.

So far i've basically been piecing together the code from Mike Beam's
Cocoa in a Nutshell book and his Cocoa column over at oreilly
([url]http://www.macdevcenter.com/pub/ct/37[/url]) for simple server/client
networking (the client side). I can make a connection, i can make a
NSFileHandle for it to put the server messages into, i can register
the filehandle with the notification center and read it in the
background, i can get it to call the correct function when a
notification is posted, but the NSData object the notification returns
is ALWAYS empty no matter what i do.

I could post all of my code, but basically i have a IRCController
class, and a NSWindowController sub class. The IRCController has an
NSSocketPort and some helper variables for connection (host, ip, port,
name, pass), and a connect method that comes down to this:

sockPort = [[NSSocketPort alloc] initRemoteWithTCPPort:6667
host:"123.456.789.001"];
clientSocket = [[NSFileHandle alloc] initWithFileDescriptor:[sockPort
socket]];

where sockPort (NSSocketPort) and clientSocket (NSFileHandle) are
class member variables. The window controller class, which has an
instance of the IRC object, has a connect method to call the IRC
connect method:

-(void)connect
{
connection = [ /* initialize the IRC object here with stuff */];
[connection connect]; // run the above connect method
clientSocket = [connection socketFileHandle]; //get a pointer to
that file handle
[clientSocket retain];
// do notification stuff
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:selector(receiveMessage:)
name:NSFileHandleReadCompletionNotification
object:clientSocket];
// set up for read
[clientSocket readInBackgroundAndNotify];
}

Finally there is a read method in the window controller class called
when an notification is triggered:

-(void)receiveMessage:(NSNotification *)notification
{
// get the new data item
NSData * messageData = [[notification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
// check for empty data (server might have disconnected or sent
empty message
if ( [messageData length] == 0 ) {
NSLog("server message was empty");
[clientSocket readInBackgroundAndNotify];
return;
}
//format and post tring to the text field in the interface
NSString * message = [NSString stringWithUTF8String:[messageData
bytes]];
[self postMessage:message];
//read next message
[clientSocket readInBackgroundAndNotify];
}

The if statement above is always taken. I read that an empty data
item could mean the connection was closed by the server, but i get
them repeatedly, not just one. I have tried commenting out the IRC
class stuff and putting this all in the same file/class, i have tried
scraping the NSSocketPort stuff for pure BSD sockets (but keeping the
NSFileHandle), all with the same result. I've also tried different
servers, and i know with an IRC client (and even telnet) the servers
I'm trying for work.

Any help would be greatly appreciated.