How to stop listening on a port?
How to stop listening on a port?
- Subject: How to stop listening on a port?
- From: John Anderson <email@hidden>
- Date: Sat, 9 Nov 2002 18:24:16 -0800
I'm using BSD sockets to listen on a TCP port... and all is working
fine, once. After the first connection, I'm having no luck. Since I'm
supporting connections over either serial or TCP, I need to instantiate
a new listening socket each time. I use this code to start listening on
the socket:
int newtonFileDescriptor;
struct sockaddr_in serverAddress;
NSFileHandle *socketHandle = nil;
int reuseAddr = 1;
int optlen;
if((newtonFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(thePort);
optlen = sizeof(reuseAddr);
setsockopt(newtonFileDescriptor, SOL_SOCKET, SO_REUSEADDR,
&reuseAddr, optlen);
if (bind(newtonFileDescriptor, (struct sockaddr *)&serverAddress,
sizeof(serverAddress)) >= 0) {
if (listen(newtonFileDescriptor, 0) == 0) {
socketHandle = [[NSFileHandle alloc]
initWithFileDescriptor:newtonFileDescriptor closeOnDealloc:YES];
[self setMyNewtonFileHandle:socketHandle];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(gotConnection:)
name:NSFileHandleConnectionAcceptedNotification
object:myNewtonFileHandle];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dataReceived:)
name:NSFileHandleReadCompletionNotification object:myNewtonFileHandle];
[socketHandle acceptConnectionInBackgroundAndNotify];
}
}
}
The problem is that when I release this object and instantiate a new
one, the client app connects but doesn't interact with the new app at
all. If I have the app set up to refuse connections (nothing allocated)
then it's still accepting connections. The file handle should get
closed when I release the file handle, right? How do I stop the listen?
John Anderson
everchanging
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.