• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Creating an NSSocketPort on an available port?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Creating an NSSocketPort on an available port?


  • Subject: Creating an NSSocketPort on an available port?
  • From: "Mac QA" <email@hidden>
  • Date: Tue, 11 Mar 2008 03:14:29 -0400

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

  • Follow-Ups:
    • Re: Creating an NSSocketPort on an available port?
      • From: Jean-Daniel Dupas <email@hidden>
    • Re: Creating an NSSocketPort on an available port?
      • From: "Kyle Sluder" <email@hidden>
  • Prev by Date: Re: How to run progress bar in a separate thread
  • Next by Date: Re: Creating an NSSocketPort on an available port?
  • Previous by thread: Re: Cocoa way to get list of user accounts?
  • Next by thread: Re: Creating an NSSocketPort on an available port?
  • Index(es):
    • Date
    • Thread