Re: CFSocket example?
Re: CFSocket example?
- Subject: Re: CFSocket example?
- From: Glenn <email@hidden>
- Date: Tue, 3 Sep 2002 23:08:38 -0600
I just found the EDCommon frameworks, which you might want to look
into. They implement some socket objects like socket->ip
socket->tcp/upd socket. This is done by subclassing or adding stuff to
the NSFileHandle class. They also have a Stream class like CFStream.
You might want to go this route if you want to stay purely in
cocoa/objective-c. However, you'll almost definitely have to go
multi-threaded I would think. CFSocket and CFStream allow you to use
the runloop to be notified of data available instead of
polling/blocking, without having to create another thread to handle it.
Here's some code of mine to create a CFStream socket pair. This is
within an objective-c class I'm working on. "inStream" is a
CFReadStreamRef, "outStream" is a CFWriteStreamRef ivar in my class.
The C callbacks are below the code. I started out using CFSocket, but
then moved to CFStream once I figured out what it was doing. However,
the EDCommon framework looks nice too, because I don't think going
multi-threaded in my type of application will be too hard, and I might
not be able to avoid it anyways. Good luck
{
BOOL didSet;
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFStreamClientContext streamCtxt = {0, self, NULL, NULL, NULL};
// set the client on the read stream
didSet = CFReadStreamSetClient([self inStream],
kReadEvents,
(CFReadStreamClientCallBack)&_ReadStreamCallBack,
&streamCtxt);
// fail if unable to set client
if (!didSet)
break;
// set the client on the write stream
didSet = CFWriteStreamSetClient([self outStream],
kWriteEvents,
(CFWriteStreamClientCallBack)&_WriteStreamCallBack,
&streamCtxt);
// Fail if unable to set the client.
if (!didSet)
break;
// schedule stream on main runloop
CFReadStreamScheduleWithRunLoop([self inStream], runLoop,
kCFRunLoopCommonModes);
CFWriteStreamScheduleWithRunLoop([self outStream], runLoop,
kCFRunLoopCommonModes);
// Open the stream for reading.
if (!CFReadStreamOpen([self inStream]))
break;
// Open the stream for writing.
if (!CFWriteStreamOpen([self outStream]))
break;
}
/*
callback for the read stream
*/
static void _ReadStreamCallBack(CFReadStreamRef inStream,
CFStreamEventType type, GOFTPController *context) {
// check if the stream we were given is the same one in the
controller
assert(inStream == [context inStream]);
// go to callback handler in obj-c
[context handleInStreamCallbackWithType:type];
}
/*
callback for write stream
*/
static void _WriteStreamCallBack(CFWriteStreamRef outStream,
CFStreamEventType type, GOFTPController* context) {
// check if the stream we were given is the same one in the
controller
assert(outStream == [context outStream]);
// go to callback handler in obj-c
[context handleOutStreamCallbackWithType:type];
}
On Tuesday, September 3, 2002, at 04:12 PM, Edward Fink wrote:
>
What I plan on doing is making a connection to an IRC server(I have
>
done this previously in Java and VB). I am trying to figure out what
>
is the best way to attempt this (BSD Sockets,CFSocket,CFNetwork,
>
CFStream, etc) and some documentation on how to implement whichever
>
solution. I actually have searched the archives and it is actually
>
one of the few places anywhere that I have found any information at
>
all on CFSocket....many of which you indeed have posted. However, a
>
lot of the examples I have found are not easy to follow since I am new
>
to Objective-C/Cocoa programming. Meaning...a lot of the examples I
>
have seen(not all)...seem to just be a dump of undocumented code that
>
supposedly "works". Which is great I guess but doesn't really help
>
the "person new to cocoa" such as myself. I'm hoping to find a well
>
documented example of how I should attempt this type of a connection.
>
>
Thanks
>
>
>
>
On Tuesday, September 3, 2002, at 01:31 PM, Douglas Davidson wrote:
>
>
>
>
> On Monday, September 2, 2002, at 05:38 PM, Edward Fink wrote:
>
>
>
>> Can someone point me to a good tcp CFSocket example? Or does someone
>
>> want to share an example? I've looked everywhere...but
>
>> documentation on this seems to be very light.
>
>
>
> What do you plan on doing with the socket? I believe I've posted a
>
> number of examples to this list--check the archives--but only for
>
> certain usage patterns. Also, you should check the CFNetwork
>
> examples--for many things you will want to use a CFStream of one sort
>
> or another rather than a CFSocket directly.
>
>
>
> Douglas Davidson
>
_______________________________________________
>
macnetworkprog mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.