Re: reading data from a UDP port - questions
Re: reading data from a UDP port - questions
- Subject: Re: reading data from a UDP port - questions
- From: "Rob Napier" <email@hidden>
- Date: Tue, 18 Mar 2008 12:21:34 -0400
Your NSLog() is giving unexpected results because you are using %s.
This is a cstring (not NSString), which is null-terminated. What you
probably mean is this (at least for testing purposes):
NSLog (@"socketCallback (DMX event). >%@< %i bytes", msg, [msg length]);
This will print the hex codes for the data in msg (so you don't get
garbage in your output). If you want an NSString of the actual bytes,
you can do that with NSString's initWithData:encoding. This *might* be
useful for handing to an NSScanner, but I suspect you'll be happier
hand-rolling the parser.
You either want to stay in NSData for this, or go to raw C. You
probably don't want to mess with NSString. NSData will let you pull
any bytes out you want in order to create a parser.
As mentioned before, using [note object] will get this back out of
your notification.
-Rob
On Sun, Mar 16, 2008 at 3:25 AM, Jason Ellemor <email@hidden> wrote:
> Hi,
>
> I have a device which basically outputs a UDP data stream. FYI the
> protocol specs are at : http://www.enttec.com/docs/enttec_protocol.pdf
>
> I found a really helpful post on the forum here titled : Re: Listen
> on a UDP Port( http://lists.apple.com/archives/cocoa-dev/2008/Feb/
> msg01180.html ) which has really helped me build the framework.
>
> However, I am stuck in 2 ways now - being pretty new to Cocoa also
> doesn't help.
>
> I guess, I don't fully understand the code posted enough because I
> can't seem to access the dictionary object created and posted to the
> notifications with the previous example. (FYI my actual code is in
> appendix 1 below)
> [[NSNotificationCenter defaultCenter]
> postNotificationName:@"NSFileHandleReadCompletionNotification"
> object:[NSDictionary dictionaryWithObject:[NSData dataWithBytes:&msg
> length:(n * sizeof(u_char))]
>
> Secondly, the device has "Null" bytes in the output stream and I am
> guessing if I get the data into a NSString object, when trying to
> parse the string, it will terminate because of the Null. And I know
> that the start of the UDP message (based on the protocol) is
> ESDD<<Null or 00 hex>>.... and confirmed with a packet sniffer app
> too (appendix 3), so I can see that when treating as a string, the
> data will be truncated at the first "Null".
>
> So if anyone could also help with an idea on how to work with such a
> data stream, I'd be really grateful.
>
> many thanks
>
>
> Jason
>
>
> 1. SocketCall Back Code
> static void socketCallback(CFSocketRef cfSocket, CFSocketCallBackType
> type, CFDataRef address, const void *data, void *userInfo)
> {
> u_char msg[MAX_UDP_DATAGRAM_SIZE];
> struct sockaddr_in from_addr;
> socklen_t addr_len = sizeof(struct sockaddr_in);
> size_t n = recvfrom(CFSocketGetNative(cfSocket), (void *)&msg,
> MAX_UDP_DATAGRAM_SIZE, 0, (struct sockaddr *)&from_addr, &addr_len);
>
> NSLog (@"socketCallback (DMX event). >%s< %i bytes", &msg, (n *
> sizeof(u_char)));
>
> [[NSNotificationCenter defaultCenter]
> postNotificationName:@"NSFileHandleReadCompletionNotification"
> object:[NSDictionary
> dictionaryWithObject:[NSData dataWithBytes:&msg
>
> length:(n * sizeof(u_char))]
>
> forKey:@"NSFileHandleNotificationDataItem"]];
> // How do I retrieve the data from this object in my notification
> subscription code?
> }
>
>
> 2. NS Log output of the above:
> 2008-03-16 18:12:08.214 lightapp[10014] socketCallback (DMX event).
> >ESDD< 521 bytes
>
> 3. Packet Sniffer output is:
> Headerlength: 522 bytes, Data follows:
>
> 00000 45 53 44 44 00 00 01 02 00 00 03 05 00 00 05 05
> ESDD............
> 00010 00 00 03 03 c6 39 00 00 00 00 02 02 04 04 00 05 .....
> 9..........
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden