• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: reading data from a UDP port - questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading data from a UDP port - questions


  • Subject: Re: reading data from a UDP port - questions
  • From: Jens Alfke <email@hidden>
  • Date: Sun, 16 Mar 2008 08:27:08 -0700


On 16 Mar '08, at 12:25 AM, Jason Ellemor wrote:

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.

The code is sending the dictionary as the object of the notification. So your observer method would access it as [n object]:


- (void) yourObserver: (NSNotfication*)n
{
NSDictionary *dict = [n object];
NSData *packet = [dict objectForKey: @"NSFileHandleNotificationDataItem"];
...
}


However, I would suggest that you *not* use the existing notification name NSFileHandleReadCompletionNotification for this, because your method would also receive those notifications posted by any NSFileHandles in your application, and in that case the notification object would be the NSFileHandle instance, not a dictionary.
Instead, make up your own notification name, and post and listen for that.


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.

(1) The code you posted doesn't put the data in an NSString; it's in an NSData. Which is appropriate if it consists of binary data. If there are pieces of it that are strings, you can extract those bits out of the NSData and convert them to NSStrings.


(2) But in any case, NSString doesn't mind embedded null bytes. (It's not null-terminated like a C string; the string object remembers the length separately.) That doesn't mean you should store binary data in an NSString, though. NSData is better for that.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

  • Follow-Ups:
    • Re: reading data from a UDP port - questions
      • From: Benjamin Stiglitz <email@hidden>
References: 
 >reading data from a UDP port - questions (From: Jason Ellemor <email@hidden>)

  • Prev by Date: Re: Convert NSString to FSRef
  • Next by Date: Re: Fullscreen NSWindow or NSView?
  • Previous by thread: reading data from a UDP port - questions
  • Next by thread: Re: reading data from a UDP port - questions
  • Index(es):
    • Date
    • Thread