Re: [Q] CoreFoundation functions or Cocoa classes to make a server socket?
Re: [Q] CoreFoundation functions or Cocoa classes to make a server socket?
- Subject: Re: [Q] CoreFoundation functions or Cocoa classes to make a server socket?
- From: JongAm Park <email@hidden>
- Date: Thu, 29 Jan 2009 07:10:12 -0800
Hello,
Thanks for pointing out that.
At first I also thought so, and there should be reason Apple didn't
enlist it under general network topic.
However, it seems to me that the NSSocketPort is implemented on top of
CFSocket for server socket.
It seems to work, although I'm not sure if it can be broken someday.
Also, there is an article about it at MacDevCenter of the O'reilly.
http://www.macdevcenter.com/pub/a/mac/2006/11/14/how-to-write-a-cocoa-web-server.html
------------------< From the article >----------------
The Easy Way to Sockets
To receive communications, we need to create an entry point in our
process where data can flow in, and bind it to the IP address of our
computer and the port number we have chosen. Such entry points are known
as BSD sockets, or simply sockets, and are identified via file descriptors.
Again, Cocoa has a class that handles the necessary details for us. This
class is known by the not so surprising name of |NSSocketPort|. This
time, however, the gotcha is that its documentation is buried deeply
within the Distributed Objects API, so much so that some developers even
doubt that it can be used reliably for purposes other than that of
serving the DO architecture. Rest assured that |NSSocketPort| will do a
nice job for us. For all those still in doubt, the end of this article
shows how to replace |NSSocketPort| with direct Unix calls and why one
might want to do so.
To obtain our sought for file descriptor |fd| we use:
|NSSocketPort *socketPort;
socketPort = [[NSSocketPort alloc initWithTCPPort:PORT_NUMBER];
int fd = [socketPort socket];|
|PORT_NUMBER| is the port number of our service (0...65535). It is
important not to release |socketPort| before being finished with
|fileHandle|.
Now that we have established a connection to the outside world, it would
be nice if we could actually start accepting incoming connections. And
we are only one method call away from reaching this goal:
|[fileHandle acceptConnectionInBackgroundAndNotify];|
---------------------------------------------------------------------
But, anyway I will use CFNetwork calls as well as some NS... helpers,
because Core Foundation routines seems to be more flexible.
Thank you.
Jens Alfke wrote:
On Jan 28, 2009, at 2:48 PM, JongAm Park wrote:
I don't know if the NSSocketPort will suit my needs well, but it was
buries in distributed object guide.
NSSocketPort is part of the implementation of Distributed Objects. It
is not for general-purpose networking, so I wouldn't recommend trying
to use it.
There isn't any public built-in Objective-C class that you can use to
run a listener socket; you have to use some CFNetwork (or BSD sockets)
calls.
—Jens
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden