I’m trying to add a simple feature to my app, it should simply send a pre-formatted string to another device, very much like WiTap sample code does. It sounds like a very trivial thing to do but I cannot get it to work ;(. How can I modify WiTap to send a string instead of a single int?
Any pointers to good tutorials would be great.
I did look at SimpleNetworkStreams sample, but it went way over my head as I’m only looking to send a string (NSString, char[], don’t have a preference) and not a file.
Looked at this example too: How to add data for NSOutputStream? but that did not fully help either.
I have since figured it out and decided to answer my own question here for the benefit of those who are in similar situation.
Whenever I want to send any string, I use this helper function I created:
On the receiving side it looks like this:
The buffer is defined as:
60,000 is fairly arbitrary, you can change it to suit your needs.
A few notes about the above. While it is safe to make the buffer for these strings fairly large, you are never guaranteed to receive your string in one go. In the actual app you should carefully design a certain protocol that you can rely on to check if you received the whole string and combine strings received during subsequent
NSStreamEvents if necessary.