Not getting socket connected notification from NSFileHandle
Not getting socket connected notification from NSFileHandle
- Subject: Not getting socket connected notification from NSFileHandle
- From: Christian Hansson <email@hidden>
- Date: Sun, 2 Jan 2005 09:22:59 -0800
I have been using straight BSD sockets for network communication for quite some time now, and wanted to try and use the 'nicer' way using the NSFileHandle, however I cannot seem
to get the notification of when a socket as connected, using the code below, either creating the socket from BSD style C functions, or using NSSocketPort. I know the notification handler is working since firing the notification manually (as seen in the commented out code) does give me the notification. I am sure there is something really stupid I have overlooked, if anyone could point me in the right direction that would be more than appareciated.
<CMH/>
<x-tad-bigger>@implementation</x-tad-bigger><x-tad-bigger> server
- (</x-tad-bigger><x-tad-bigger>void</x-tad-bigger><x-tad-bigger>)acceptConnection:(NSNotification *)notification
{
NSLog(</x-tad-bigger><x-tad-bigger>@"Got the damn thing"</x-tad-bigger><x-tad-bigger>);
NSFileHandle *remoteFH = [[notification userInfo] objectForKey:NSFileHandleNotificationFileHandleItem];
</x-tad-bigger><x-tad-bigger>// Verarbeitung der Connection }</x-tad-bigger><x-tad-bigger>
}
- (</x-tad-bigger><x-tad-bigger>void</x-tad-bigger><x-tad-bigger>)doServer
{
</x-tad-bigger><x-tad-bigger>/*
int sfd = socket(AF_INET, SOCK_STREAM,0);
struct sockaddr_in sockAdr;
sockAdr.sin_family = AF_INET;
sockAdr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAdr.sin_port = htonl(31415);
if(-1 == bind(sfd, &sockAdr, sizeof(sockAdr)))
{
NSLog(@"Bind failed");
return;
}
listen(sfd, 5); */</x-tad-bigger><x-tad-bigger>
NSSocketPort *socketPort = [[NSSocketPort alloc] initWithTCPPort:</x-tad-bigger><x-tad-bigger>31415</x-tad-bigger><x-tad-bigger>];
</x-tad-bigger><x-tad-bigger>// now listen for an incoming connection</x-tad-bigger><x-tad-bigger>
NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:[socketPort socket]];
NSNotification *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:</x-tad-bigger><x-tad-bigger>self</x-tad-bigger><x-tad-bigger>
selector:</x-tad-bigger><x-tad-bigger>@selector</x-tad-bigger><x-tad-bigger>(acceptConnection:)
name:NSFileHandleConnectionAcceptedNotification
object:</x-tad-bigger><x-tad-bigger>nil</x-tad-bigger><x-tad-bigger>];
[fh acceptConnectionInBackgroundAndNotify];
</x-tad-bigger><x-tad-bigger>//[nc postNotificationName:NSFileHandleConnectionAcceptedNotification object:fh];</x-tad-bigger><x-tad-bigger>
}
</x-tad-bigger><x-tad-bigger>@end</x-tad-bigger><x-tad-bigger>
</x-tad-bigger>
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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