title: iPhone Socket Stream Behavior - Sending bytes to a Server.. NSStream/CFStream
body: <p>Hello all and thanks for looking,</p>
  
  <p><strong>Explanation of Environment</strong></p>
  
  <p>I'm working on an app that communicates with an arduino (server, for all practical purposes) that is listening on port 2000. Connecting to the arduino is done via an ad-hoc network, so the IP address of the "server" is 169.254.1.1. Upon connecting, a client will receive an IP in the 169.254.0.0 subnet.</p>
  
  <p><strong>My Current Code</strong> </p>
  
  <p>After a bit of tinkering (this is my first go at Objective C), I've managed to open a socket and establish a read/write stream between the iPhone and Arduino with the following code...</p>
  
  <p>In <code>(void)viewDidLoad</code> I call
      <code>[self initNetworkCommunication];</code></p>
  
  <p>And define that as follows...</p>
  
  <pre><code>-(void) initNetworkCommunication 
  {
      CFReadStreamRef readStream;
      CFWriteStreamRef writeStream;
  
      CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"169.254.1.1", 2000, &amp;readStream, &amp;writeStream);
  
  
      inputStream = (__bridge NSInputStream *)readStream;
      outputStream = (__bridge NSOutputStream *)writeStream;
      [inputStream setDelegate:self];
      [outputStream setDelegate:self];
      [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [inputStream open];
      [outputStream open];
  }
  </code></pre>
  
  <p>I have created this function to send the defined byte, however I'm not sure if I am doing this correctly. An explanation of sending bytes in hex/binary format would be super helpful as I haven't found a good tutorial/code snippet in my research. This works on a timer (for holding down a button) which works fine. I am specifically wondering if I am sending the byte correctly. </p>
  
  <pre><code>- (void)sendByte:(NSTimer *)timer
  {
      Byte allDown = 0x00F0;
      NSMutableData *sendData = [NSMutableData dataWithLength:sizeof(allDown)];
      [sendData appendBytes:&amp;allDown length:sizeof(allDown)];
      [outputStream write:[sendData bytes] maxLength:[sendData length]];
  }
  </code></pre>
  
  <p><strong>My Problem / Question(s)</strong></p>
  
  <p>Upon building/testing this app on my iPhone, I connected to the Arduino and pressed the button attached to the sendByte function and saw my indicator LED's (on the arduino) light up like a christmas tree. The only way those LED's turn on is if the arduino is receiving certain bytes on port 2000. This is strange, because I thought that I was only sending one specific byte 0x00F0. In the above code, is there any reason that bytes other than 0x00F0 would be sent? i.e. does opening up the socket and read/write streams send any bytes?</p>
  
  <p>Furthermore, an example of sending binary data (i.e. 0001 or 10000000) or hex data (i.e. 0F) to socket/stream would be greatly appreciated. If I'm doing this completely wrong, or if there is a more simple method of sending bytes to a server, I'd be interested in learning that as well!</p>
  
  <p>If any additional information is required, please ask and I will do my best to provide.</p>
  
  <p>Thank you for taking the time to read this, I'm looking forward to any responses!</p>
  
  <p>--John</p>