Re: CFSocketCreateRunLoopSource crashes
Re: CFSocketCreateRunLoopSource crashes
- Subject: Re: CFSocketCreateRunLoopSource crashes
- From: Quinn <email@hidden>
- Date: Tue, 26 Aug 2008 10:52:03 +0100
At 21:42 +0800 24/8/08, Liwei wrote:
1. After doing some searching, it seems that to be able to open a udp
broadcast socket (255.255.255.255), I need to set SO_BROADCAST to 1.
Now, I didn't even know there were such socket options that I could
set. Are there other recommended options that I should know of for
setting up a udp broadcast send/receive socket?
None that springs to mind. But sockets programming is a complicated
topic and I'm unlikely to recall all of the nuances. I strongly
recommend that you get a copy of "UNIX Network Programming". If
you're in to networking, it's the best $N that you can spend.
<http://www.unpbook.com/>
2. Okay, due to my lack of experience in Core Foundation coding, what
is the recommended way of converting char strings to CFData form used
in CFSocketSendData? I'm currently just casting them into UInt8 and
using CFDataCreate, which isn't safe due to incompatible signess.
Typically I would do the following:
dataRef = CFDataCreate(NULL, (const UInt8 *) str, strlen(str));
You might need a + 1 if you want the string to contain the trailing null.
This will make a copy. If you don't want to make a copy, use
CFDataCreateWithBytesNoCopy, but be aware that that routine has some
serious gotchas (a NULL bytesDeallocator doesn't do what you think it
might do). See the documentation for details.
Also, keep in mind that with CFSocket it's perfectly reasonable to
make sockets calls directly on the underlying socket. For example:
bytesWritten = write( (int) CFSocketGetNative(sockRef), ... );
3. In fact, is there some cheat sheet that I can refer to for
converting different data types to CFData?
Once you understand the CFData model (see below) this probably won't
be necessary.
4. What is CFData for anyway? I'm guessing some resizable linked-list
based storage (from the functions I see in the documentation)?
CFData represents an immutable contiguous range of bytes.
CFMutableData is the mutable variant; this has a capacity (the size
of the underlying buffer) and a length (the amount of valid data in
the buffer). That's it! There's no magic, no complexity. For
example, if you call CFDataAppendBytes and exceed the capacity of the
buffer, CFData will allocate a new, bigger buffer and copy the data
to that.
For more info, see the links posted by other folk.
5. Is a socket unidirectional or bidirectional? Do I need to open a
separate socket for listening and writing like streams?
A socket is bidirection. CFSocket does not restrict that.
6. How can I monitor a udp broadcast? Currently I can only write to
it, and read from a udp packet directed explicitely to my ip address,
but not read from a broadcast.
I don't think you need to do anything to receive broadcasts. The
only gotcha I'm aware of is that you must bind to INADDR_ANY. If you
bind to a specific IP address, you only receive messages sent
directly to that address.
* * *
What are you doing with broadcast sockets? Nine times out of ten
broadcast sockets are the wrong solution. The right solutions are:
o Bonjour, if you just want to do service discovery.
o multicast sockets
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
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