title: Trying to send and receive message using protobuf in java,but got error: Protocol message contained an invalid tag (zero)
body: <p>The problem happened at server end, parseDelimitedFrom() calling.</p>
  
  <p>Client End:</p>
  
  <pre><code>    C2SGainCard.Builder s = C2SGainCard.newBuilder();
      C2SGainCard c2s = s.build();
  
      GameRequest.Builder  reqBuilder=GameRequest.newBuilder();
      reqBuilder.setBody(c2s.toByteString());
      reqBuilder.setName(C2SGainCard.class.getSimpleName());
      reqBuilder.setPlayerId("3");
      GameRequest request=reqBuilder.build();
  
      DataOutputStream os = new DataOutputStream(socket.getOutputStream());
      os.write(request.toByteArray());
      os.flush();
      os.close();
      socket.close();
  </code></pre>
  
  <p>Server End:</p>
  
  <pre><code>    try{
          DataInputStream is = new DataInputStream(socket.getInputStream());
          GameRequest gameRequest = GameRequest.parseDelimitedFrom(is);
      }catch(Exception ex){
          System.out.println(ex.getMessage());
      }
  </code></pre>
  
  <p>And here is the another similar question:</p>
  
  <p>Client End:</p>
  
  <pre><code>    C2SSell.Builder s = C2SSell.newBuilder();
      CardOnSell.Builder cardOnSell = CardOnSell.newBuilder();
      cardOnSell.setId(1);
      cardOnSell.setPlayerId(3);
      cardOnSell.setCardId(1);
      cardOnSell.setCurrentPrice(111);
      cardOnSell.setFixedPrice(555);
      cardOnSell.setDescription("cao");
  
      s.setCardOnSell(cardOnSell.build());
  
      C2SSell c2s = s.build(); 
  </code></pre>
  
  <p>socket processing was the same.</p>
  
  <p>Server End:</p>
  
  <pre><code>    DataInputStream is = new DataInputStream(socket.getInputStream());
      byte[] b = new byte[1024];
      int len=is.read(b);
      String as = new String(b, 0, len);
      GameRequest gameRequest=GameRequest.parseFrom(as.getBytes());
  </code></pre>
  
  <p>Turns out that the server end will break at the parseFrom() calling again.But when I comment two lines:</p>
  
  <pre><code>cardOnSell.setCurrentPrice(111);
  cardOnSell.setFixedPrice(555);
  </code></pre>
  
  <p>on the client End,the parseFrom() calling just worked without problem. At frist I suspected the .proto file has some problems and it's been proved there is no problem there... So how does this problem come? It that because I miss some data before the parseFrom() calling?</p>