• 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: Do NSStreams require data serialization?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Do NSStreams require data serialization?


  • Subject: Re: Do NSStreams require data serialization?
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 10 Jul 2014 13:52:58 -0700


On Jul 10, 2014, at 1:13 PM, Carl Hoefs <email@hidden> wrote:

I'm a bit confused about sending raw data bytes (large blocks of binary ints and floats) over an NSStream. In the Apple NSStream documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html), it's shown how to send the bytes contained within an NSData object directly over the stream, without any prior serialization or conversion. However when I do that, the data seem to become corrupted. 

You’re doing something wrong then. NSStreams are just raw streams of bytes; they don’t do any kind of encoding or serialization framing or conversion at all. They’re exactly the same abstraction as found in most other platforms (java.io.Stream, FILE*, Unix file handles, etc.)

Just a guess, but: One common newbie mistake people make with streams is thinking that if you write data in blocks of specific sizes, then on the other end of the stream your read calls will get blocks of the same sizes. For example if you first write 100 bytes, then write 25000 bytes, then write 8000 bytes; sometimes people think that the first read call will get 100 bytes, the second read call will get 25000, and the third read call will get 8000. This isn’t true. The stream is just an undifferentiated series of bytes, and the number of bytes you get on any specific read call is based only on things like the way the stream is transmitted (e.g. in network packets) and the sizes of the buffers on either end.

Have you looked at Apple’s sample code for networking? The PictureSharing example shows how to send and receive images between computers; that seems very similar to what you’re doing.

—Jens
 _______________________________________________
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

References: 
 >Do NSStreams require data serialization? (From: Carl Hoefs <email@hidden>)

  • Prev by Date: Re: Re(2): Is there a transactional data limit with NSStreams?
  • Next by Date: Forcing DNS-SD/Bonjour to recreate list of servers for a browse operation
  • Previous by thread: Do NSStreams require data serialization?
  • Next by thread: Forcing DNS-SD/Bonjour to recreate list of servers for a browse operation
  • Index(es):
    • Date
    • Thread