Re: Making an NSConnection from a BSD socket
Re: Making an NSConnection from a BSD socket
- Subject: Re: Making an NSConnection from a BSD socket
- From: j o a r <email@hidden>
- Date: Fri, 26 Mar 2004 07:57:39 +0100
On 2004-03-26, at 03.42, John Scalo wrote:
>
Am I missing something? The client works fine when I set up the port
>
via [[NSSocketPort alloc] initWithTCPPort: MYPORT] however then I
>
can't reuse the port if/when the server dies for 10 minutes or so.
Here's a snippet of code I used in an old project of mine. Perhaps you
can find something useful. Especially I think you need to reuse the
address, and not only the port. I'm no expert on the topic though.
================================================================
NSSocketPort *receivePort = nil;
NS_DURING
receivePort = [[NSSocketPort alloc] initWithTCPPort: SERVER_PORT];
NS_HANDLER
[receivePort release];
receivePort = nil;
[Log alert: @"Error: Failed to bind to TCP port"];
NS_ENDHANDLER
if (receivePort != nil)
{
serverConnection = [[NSConnection alloc] initWithReceivePort:
receivePort sendPort: nil];
[receivePort release];
[serverConnection setDelegate: self];
[serverConnection setRootObject: self];
[[NSRunLoop currentRunLoop] addPort: receivePort forMode:
NSConnectionReplyMode];
// Set a flag so that we can reuse the port immediately after
releasing the socket object
{
int socket = [receivePort socket];
int flag = 1;
if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (void *)&flag,
sizeof(flag)) < 0)
[Log error: @"Failed to set socket address reuse"];
if (setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, (void *)&flag,
sizeof(flag)) < 0)
[Log error: @"Failed to set socket port reuse"];
}
// Register for notifications
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(connectionDidInitialize:)
name: NSConnectionDidInitializeNotification
object: nil];
}
================================================================
j o a r
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.