Re: How to stop listening on a port?
Re: How to stop listening on a port?
- Subject: Re: How to stop listening on a port?
- From: John Anderson <email@hidden>
- Date: Mon, 11 Nov 2002 14:05:39 -0800
Oh, I think I see the problem then. I was getting the socket from the
acceptance notification. So what I need to close is the original
listening socket. Makes sense.
John Anderson
everchanging
On Monday, November 11, 2002, at 01:38 AM, Malte Tancred wrote:
On sunday, nov 10, 2002, at 03:24 Europe/Stockholm, John Anderson
wrote:
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.
First of all, I'm not sure what you mean with "all is working". From
what
I see the only thing that should work is the initial connection
attempt.
I doubt the data exchange between your app and the client is working.
Have
you really tested that part?
Second, why do you have to create a new listening socket every time? Do
you use different ports for every connection?
int newtonFileDescriptor;
NSFileHandle *socketHandle = nil;
...
if((newtonFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
...
if (bind(newtonFileDescriptor, (struct sockaddr *)&serverAddress,
sizeof(serverAddress)) >= 0) {
if (listen(newtonFileDescriptor, 0) == 0) {
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(gotConnection:)
name:NSFileHandleConnectionAcceptedNotification
object:myNewtonFileHandle];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dataReceived:)
name:NSFileHandleReadCompletionNotification
object:myNewtonFileHandle];
This doesn't make sense to me.
If I'm not mistaken you are trying to perform two different things
with the listening socket. The listening socket will do only one
thing, namely accept new connections. From the documentation of
NSFileHandle's acceptConnectionInBackgroundAndNotify:
"The notification includes as data a userInfo dictionary
containing the created NSFileHandle; access this object
using the NSFileHandleNotificationFileHandleItem key."
You should register for read completion notifications on this newly
created file handle, not the original listening socket. The listening
socket can only be used for accepting (ie creating) new sockets.
Sockets created when "accepting" on a listening socket represent the
actual connection between your application and the client application.
How do I stop the listen?
You close the listening socket.
Hope this helps,
Malte
_______________________________________________
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.