Re: Bonjour and multi-meg files
Re: Bonjour and multi-meg files
- Subject: Re: Bonjour and multi-meg files
- From: Ken Tozier <email@hidden>
- Date: Thu, 15 Mar 2007 19:11:05 -0400
After playing around a bit with the picture sharing example app, I
encapsulated some of the code into distinct methods. It still works
the same but I'd like to further adapt it by converting the low level
BSD calls in the example to NSSocketPort and NSNetService. I don't
know the first thing about sockets so the low level code in the
following function is Greek to me. The lines I'm most confused by are:
if((fdForListening = socket(AF_INET, SOCK_STREAM, 0)) > 0)
if(bind(fdForListening, (struct sockaddr *)&serverAddress, sizeof
(serverAddress)) >= 0)
if(getsockname(fdForListening, (struct sockaddr *)&serverAddress,
&namelen) >= 0)
if(listen(fdForListening, 1) == 0)
How would one convert these for use in an NSSocketPort initialization?
Thanks for any help
Ken
Here's the code segment I'm having the most difficulty understanding
- (NSFileHandle *) listeningSocket:(unsigned short *) inChosenPort
{
int fdForListening;
struct sockaddr_in serverAddress;
socklen_t namelen = sizeof(serverAddress);
NSFileHandle *result = nil;
// init the chosen port
*inChosenPort = 0;
// 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; // allows the kernel to choose the
port for us.
if(bind(fdForListening, (struct sockaddr *)&serverAddress, sizeof
(serverAddress)) >= 0)
{
// Find out what port number was chosen for us.
if(getsockname(fdForListening, (struct sockaddr *)&serverAddress,
&namelen) >= 0)
{
*inChosenPort = ntohs(serverAddress.sin_port);
if(listen(fdForListening, 1) == 0)
{
result = [[NSFileHandle alloc]
initWithFileDescriptor: fdForListening
closeOnDealloc: YES];
}
}
else
close(fdForListening);
}
else
close(fdForListening);
}
return result;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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