title: Only one write() call sends data over socket connection
body: <p>First stackoverflow question! I've searched...I promise. I haven't found any answers to my predicament. I have...a severely aggravating problem to say the least. To make a very long story short, I am developing the infrastructure for a game where mobile applications (an Android app and an iOS app) communicate with a server using sockets to send data to a database. The back end server script (which I call BES, or Back End Server), is several thousand lines of code long. Essentially, it has a main method that accepts incoming connections to a socket and forks them off, and a method that reads the input from the socket and determines what to do with it. Most of the code lies in the methods that send and receive data from the database and sends it back to the mobile apps. All of them work fine, except for the newest method I have added. This method grabs a large amount of data from the database, encodes it as a JSON object, and sends it back to the mobile app, which also decodes it from the JSON object and does what it needs to do. My problem is that this data is very large, and most of the time does not make it across the socket in one data write. Thus, I added one additional data write into the socket that informs the app of the size of the JSON object it is about to receive. However, after this write happens, the next write sends empty data to the mobile app.</p>
  
  <p>The odd thing is, when I remove this first write that sends the size of the JSON object, the actual sending of the JSON object works fine. It's just very unreliable and I have to hope that it sends it all in one read. To add more oddity to the situation, when I make the size of the data that the second write sends a huge number, the iOS app will read it properly, but it will have the data in the middle of an otherwise empty array.</p>
  
  <p>What in the world is going on? Any insight is greatly appreciated! Below is just a basic snippet of my two write commands on the server side.</p>
  
  <p>Keep in mind that EVERYWHERE else in this script the read's and write's work fine, but this is the only place where I do 2 write operations back to back.</p>
  
  <p>The server script is on a Ubuntu server in native C using Berkeley sockets, and the iOS is using a wrapper class called AsyncSocket.</p>
  
  <pre><code>int n;
  //outputMessage contains a string that tells the mobile app how long the next message
  //(returnData) will be
  n = write(sock, outputMessage, sizeof(outputMessage));
  if(n &lt; 0)
     //error handling is here
  //returnData is a JSON encoded string (well, char[] to be exact, this is native-C)
  n = write(sock, returnData, sizeof(returnData));
  if(n &lt; 0)
     //error handling is here
  </code></pre>
  
  <p>The mobile app makes two read calls, and gets <code>outputMessage</code> just fine, but <code>returnData</code> is always just a bunch of empty data, unless I overwrite <code>sizeof(returnData)</code> to some hugely large number, in which case, the iOS will receive the data in the middle of an otherwise empty data object (NSData object, to be exact). It may also be important to note that the method I use on the iOS side in my AsyncSocket class reads data up to the length that it receives from the first write call. So if I tell it to read, say 10000 bytes, it will create an NSData object of that size and use it as the buffer when reading from the socket.</p>
  
  <p>Any help is greatly, GREATLY appreciated. Thanks in advance everyone!</p>