TCP/IP Connections
TCP/IP Connections
- Subject: TCP/IP Connections
- From: email@hidden
- Date: Sat, 22 Dec 2001 16:00:23 +0100
Hi all,
I would like to hear your suggestions on the approach I need to take
with the following problem I'm having. It seems that there is no API in
the Foundation framework that lets you do basic socket handling
(opening/closing/reading/writing) for TCP/IP connections. Now, of
course, this hasn't got to be a problem, because there seem to be some
other solutions available. However, I'm finding it quite difficult to
chose between any of them. And therefore I would like to hear from
people on this list what method they chose and why.
Before I summerize the options I found, I need to tell you that the
application I'm writing needs to communicate with other applications
written on different platforms and in different languages with a
communication protocol that has already been defined (this rules out any
DO approaches as far as I can see).
Secondly I would like to do the socket/communication handling within my
application in an asynchronous manner (especially for the arrival of
incoming data and detection of connectionloss).
And last of all, I wan't it in a (set of) class(es). I don't wan't to
copy paste a bunch of code for each application I write that uses TCP/IP
socket communication.
Here are the options that I have come up with so far:
* Using my own NSPort
Although the meaning of NSPort has been quite vague for me (and still
is) it seems that an NSPort is not just an abstraction for a
communicationport, but also for (part of) the protocol that is used for
the communication (how to encode/decode a series of objects over the
communication channel). So in order to handle my own protocol I probably
have to make my own subclass of NSPort, right? However, the part of the
documentation that tells you how to deal with making your own subclasses
and on what to do with NSConnection (for which you probably also need
your own subclass) is something I can't find.
Today I also took a brief look at a class from GNUStep (the GSTcpPort
class) which seemed to be an implementation for a basic TCP/IP
connection. However the GSTcpPort class also has it's own underlaying
protocol (first sending a header and then a list of 'messages').
Although it isn't the protocol I wan't, fortunately the source is
available, so I might be able to use it as a starting point.
* The OMNI Networking framework
This framework seems quite usefull (although I prefer to have a solution
that is based on libraries provided by Apple), but as far as I can see
it is all synchronous. If there would be a way to couple this with an
NSRunloop than I guess this might be a viable solution.
* Core Foundation framework
This is the option I have currently tried and implemented once in a
testapplication (I used CFStreamCreatePairWithSocketToHost()). Although
it is not OO (I still have to write my own OO wrappers around it) it
does what I need. I can make a connection, get an inputstream and an
outputstream, and everything can be handled asynchronous (by making use
of CFRunloop, which I retrieved from NSRunloop). However, I don't get it
why I have to have a separate stream for incomming traffic and a
separate for outgoing. Does this have to do with platform independence?
Under linux (and I guess BSD uses the same) I can create ONE r/w
filestream for a socket with fdopen().
Furthermore, the events do not seem to work properly. If I drop my
connection at the remote side I get TWO messages indicating
kCFStreamEventHasBytesAvailable with 0 bytes to read and the
kCFStreamEventEndEncountered never gets send (is this a known issue, or
am I doing something wrong here?)
* Use Carbon
I have absolutely no experience with carbon, so I haven't even looked at
this yet.
* Bugger all and write my own classes using the BSD layer
I hope it won't have to come to that, but I could also write my own
classes (much the way OMNI did) using POSIX and make it asynchronous by
using an NSTimer.
* Any other???
So there it is. There are quite some options and to RTFM each option
just to figure out which option I should chose doesn't seem a time
efficient thing to do. It also seems to me that I am not the only one
with this problem and that it is a gab in the Foundation framework. So
I'm also curious if Apple has some plans in this direction (e.g. make an
OO variant of the Core Foundation API for sockets/streams). If so, than
my current choice of method would be the one that most closely resembles
that what cocoa is going to bring in the future.
Regards,
Sander Niemeijer