title: How do I get google protocol buffer messages over a socket connection without disconnecting the client?
body: <p>I'm attempting to send a .proto message from an iPhone application to a Java server via a socket connection. However so far I'm running into an issue when it comes to the server receiving the data; it only seems to process it after the client connection has been terminated. This points to me that the data is getting sent, but the server is keeping its inputstream open and waiting for more data. Would anyone know how I might go about solving this? The current code (or at least the relevant parts) is as follows:</p>
  
  <p>iPhone:</p>
  
  <pre><code>Person *person = [[[[Person builder] setId:1] setName:@"Bob"] build];
  RequestWrapper *request = [[[RequestWrapper builder] setPerson:person] build];
  NSData *data = [request data];  
  AsyncSocket *socket = [[AsyncSocket alloc] initWithDelegate:self];
  
  if (![socket connectToHost:@"192.168.0.6" onPort:6666 error:nil]){
      [self updateLabel:@"Problem connecting to socket!"];
  } else {
      [self updateLabel:@"Sending data to server..."];
      [socket writeData:data withTimeout:-1 tag:0];
      [self updateLabel:@"Data sent, disconnecting"];
      //[socket disconnect];
  }
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>try {
          RequestWrapper wrapper = RequestWrapper.parseFrom(socket.getInputStream());
          Person person = wrapper.getPerson();
          if (person != null) {
              System.out.println("Persons name is " + person.getName());
              socket.close();
          }
  </code></pre>
  
  <p>On running this, it seems to hang on the line where the RequestWrapper is processing the inputStream. I did try replacing the socket writedata method with: </p>
  
  <pre><code>[request writeToOutputStream:[socket getCFWriteStream]];
  </code></pre>
  
  <p>(here I'm calling the gpb to write to the output stream, instead of writing the data generated to the output stream)</p>
  
  <p>Which I thought might work, however I get an error claiming that the "Protocol message contained an invalid tag (zero)". I'm fairly certain that it doesn't contain an invalid tag as the message works when sending it via the writedata method.</p>
  
  <p>Any help on the matter would be greatly appreciated!</p>
  
  <p>Cheers!</p>
  
  <p>Dan</p>
  
  <p>(EDIT: I should mention, I am using the metasyntactic gpb code; and the cocoaasyncsocket implementation)</p>