Re: Creating an NSSocketPort on an available port?
Re: Creating an NSSocketPort on an available port?
- Subject: Re: Creating an NSSocketPort on an available port?
- From: Jean-Daniel Dupas <email@hidden>
- Date: Tue, 11 Mar 2008 08:43:28 +0100
Addind those lines after your port creation should alow you to reuse
it after a quick restart:
int yes = 1;
setsockopt([receivePort socket], SOL_SOCKET, SO_REUSEADDR, (void
*)&yes, sizeof(yes));
else you will have to get the socket adress after it's creation and
extract port from it. You may have to check the port family before
doing this to ensure that this is an IPv4 socket (PF_INET or
PF_INET6). For IPv6, you will have to use 'struct sockaddr_in6'
instead of 'struct sockaddr_in'.
UInt8 port = 0;
struct sockaddr_in* addr4;
NSData *data = [receivePort address];
if (data) {
addr4 = (struct sockaddr_in *)[data bytes];
port = ntohs(addr4->sin_port);
}
Le 11 mars 08 à 08:14, Mac QA a écrit :
Hi,
I have a little program that creates an NSSocketPort for use in
Distributed Objects, and also advertises itself over Bonjour. So,
something like this...
// This server will wait for responses on port 3121
receivePort = [[NSSocketPort alloc] initWithTCPPort:3121];
connection = [NSConnection connectionWithReceivePort:receivePort
sendPort:nil];
...
// Create Bonjour Advertisement
netService = [[NSNetService alloc] initWithDomain:@"local"
type:@"_myapp._tcp."
name:hostName
port:3121];
This all works OK, except when I run, quit, and run the app quickly.
The port 3121 isn't yet available again on the second run of the
program because the socket is still in the TIME_WAIT period from the
first run of the program. So, what I would like to do is be dynamic
and listen on a random port that is available, and make my Bonjour
advertisement for that port. Yet I am having difficulty figuring out
how to accomplish that.
// Create a generic receivePort
receivePort = [[NSSocketPort alloc] init];
connection = [NSConnection connectionWithReceivePort:receivePort
sendPort:nil];
...
// Create Bonjour Advertisement
// How do I figure out the (unsigned int) value for the port to pass
here???
netService = [[NSNetService alloc] initWithDomain:@"local"
type:@"_myapp._tcp."
name:hostName
port:???];
The NSSocketPort class doesn't seem to have a way to get the port when
you just alloc & init like this. So How can you advertise over Bonjour
for a generic dynamically determined port selected at runtime?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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