How can I tell if a port is already being used?
How can I tell if a port is already being used?
- Subject: How can I tell if a port is already being used?
- From: Brian Stibi <email@hidden>
- Date: Tue, 26 Dec 2006 16:21:33 -0800
Greetings,
Forgive me ahead of time, I have taught myself how to program, so
there is a great many things I am unaware of.....
So in my code I have a subclass of NSObject that contains these
three methods among others....
- (void) startListening:(int)port
{
portNumber = port;
NSSocketPort *sockPort;
sockPort = [[NSSocketPort alloc] initWithTCPPort:port];
int sockFD = [sockPort socket];
NSFileHandle *listeningSocket;
listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:sockFD];
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(spawnChildConnection:)
name:NSFileHandleConnectionAcceptedNotification object:listeningSocket];
[listeningSocket acceptConnectionInBackgroundAndNotify];
}
- (void) spawnChildConnection:(NSNotification*)note
{
openSocket = [[note userInfo]
objectForKey:NSFileHandleNotificationFileHandleItem];
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(processClientData:)
name:NSFileHandleReadCompletionNotification object:openSocket];
[[NSNotificationCenter defaultCenter] postNotificationName:@"output"
object:@"[Connection Open]\n"];
[openSocket readInBackgroundAndNotify];
}
- (void) processClientData:(NSNotification*)note
{
NSData *data = [[note userInfo]
objectForKey:NSFileHandleNotificationDataItem];
if ([data length] == 0)
[self connectionDidDie:note];
else
{
NSString *output = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"output" object:[NSString
stringWithFormat:@"remote:%@",output]];
[[note object] readInBackgroundAndNotify];
}
}
When I call the startListening method with a port that already has a
file descriptor attached to it, it seems that
NSFileHandleConnectionAcceptedNotification gets called instantly.
Does anyone know of a (simple)method for checking if a port is
already used.
Happy New Year!
- Brian
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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