Re: CFSocketCreateRunLoopSource crashes
Re: CFSocketCreateRunLoopSource crashes
- Subject: Re: CFSocketCreateRunLoopSource crashes
- From: Liwei <email@hidden>
- Date: Sun, 24 Aug 2008 21:42:08 +0800
Ah, you're right. This time I got it!
I have a few other questions now since I can move on.
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?
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.
3. In fact, is there some cheat sheet that I can refer to for
converting different data types to CFData?
4. What is CFData for anyway? I'm guessing some resizable linked-list
based storage (from the functions I see in the documentation)?
5. Is a socket unidirectional or bidirectional? Do I need to open a
separate socket for listening and writing like streams?
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.
And yes, the test machine is running on an isolated network to prevent
evil network clog ups.
> Message: 14
> Date: Sun, 24 Aug 2008 09:23:07 -0400
> From: Jaime Magiera <email@hidden>
> Subject: Re: CFSocketCreateRunLoopSource crashes
> To: Mac Network <email@hidden>
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
>
> On Aug 24, 2008, at 5:01 AM, Liwei wrote:
>
>> Hmm... Somehow the following code crashes while trying to create the
>> runloop source:
>> struct sockaddr_in a = {0, AF_INET, htons(5555), 0};
>> CFDataRef d = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&a,
>> sizeof(struct sockaddr_in));
>> CFSocketSignature signature = {PF_INET, SOCK_DGRAM, IPPROTO_UDP, d};
>> CFSocketRef socket =
>> CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature,
>> kCFSocketDataCallBack, receiveData, NULL);
>> CFRunLoopSourceRef source =
>> CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
>> CFRunLoopRef loop = CFRunLoopGetCurrent();
>> CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
>
> Hi Liwei,
>
> I responded to you before, not sure if you saw it. Comparing your code
> with mine (which works on XCode 3.1/OSX10.5/Intel), it seems our
> sockaddr_in structs are different. The struct is defined as...
>
> struct sockaddr_in { short sin_family;
> unsigned short sin_port;
> struct in_addr sin_addr;
> char sin_zero[8]; };
>
> To help keep my structs clear and tidy, I like to explicitly set their
> members. In this case...
>
> struct sockaddr_in myAddress;
> myAddress.sin_family = AF_INET;
> myAddress.sin_port = htons(testPort);
> myAddress.sin_addr.s_addr = htonl(INADDR_ANY);
>
> Your structure is created with 4 members, which don't see to be correct.
>
> struct sockaddr_in a = {0, AF_INET, htons(5555), 0};
>
> You have an erroneous member at the beginning. So, you end up passing
> "0" as sin_family, "AF_INET" as sin_port and "htons(5555)" as
> sin_addr. (Did you intend the last member to be sin_zero?)
>
> Could it be that your code is failing due to this?
>
> Jaime Magiera
>
> Sensory Research
> http://www.sensoryresearch.net
>
>
>
> ------------------------------
_______________________________________________
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