Re: NSNetService ports
Re: NSNetService ports
- Subject: Re: NSNetService ports
- From: mmalcolm crawford <email@hidden>
- Date: Mon, 3 Nov 2003 21:56:05 -0800
On Nov 3, 2003, at 8:33 PM, Francisco Tolmasky wrote:
Ok, I'm using NSNetService to create a rendezvous app and I'm a little
confused about the port number: [...]
Is this just an arbitrary value, or is there a good way to get one?
Well-known port names are defined by IANA:
<
http://www.iana.org/assignments/port-numbers>
One of the benefits of Rendezvous is that, because service discovery
and resolution are separate, you probably don't need to use well-known
ports for your own service (obviously if you're advertising, say, an
HTTP server you'll probably want to use the usual port).
Will using htons(0) get me a good port?
See code below, from Apple's Picture Sharing example.
<
http://developer.apple.com/samplecode/Sample_Code/Networking/
PictureSharing.htm>
mmalc
/*
In order to use NSFileHandle's acceptConnectionInBackgroundAndNotify
method, we need to create a file descriptor that is itself a socket,
bind that socket, and then set it up for listening. At this point, it's
ready to be handed off to acceptConnectionInBackgroundAndNotify.
*/
if((fdForListening = 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 = 0;
// Allow the kernel to choose a random port number by passing in 0
for the port.
if (bind(fdForListening, (struct sockaddr *)&serverAddress,
namelen) < 0) {
close (fdForListening);
return;
}
// Find out what port number was chosen.
if (getsockname(fdForListening, (struct sockaddr *)&serverAddress,
&namelen) < 0) {
close(fdForListening);
return;
}
chosenPort = ntohs(serverAddress.sin_port);
_______________________________________________
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.