Take the 2-minute tour ×

The problem happened at server end, parseDelimitedFrom() calling.

Client End:

    C2SGainCard.Builder s = C2SGainCard.newBuilder();
    C2SGainCard c2s = s.build();

    GameRequest.Builder  reqBuilder=GameRequest.newBuilder();
    GameRequest request=reqBuilder.build();

    DataOutputStream os = new DataOutputStream(socket.getOutputStream());

Server End:

        DataInputStream is = new DataInputStream(socket.getInputStream());
        GameRequest gameRequest = GameRequest.parseDelimitedFrom(is);
    }catch(Exception ex){

And here is the another similar question:

Client End:

    C2SSell.Builder s = C2SSell.newBuilder();
    CardOnSell.Builder cardOnSell = CardOnSell.newBuilder();


    C2SSell c2s = s.build(); 

socket processing was the same.

Server End:

    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());

Turns out that the server end will break at the parseFrom() calling again.But when I comment two lines:


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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I can't remember exactly what format parseDelimitedFrom expects, but I suspect you just want parseFrom in your server side.

It's not clear why you're using os.write(request.toByteArray()) though, or indeed why you're creating a DataOutputStream. You should be able to use just OutputStream and InputStream, writing:


and then:

GameRequest gameRequest = GameRequest.parseFrom(socket.getInputStream());

If you do want the delimited version, you need to use writeDelimitedTo instead.

share|improve this answer
thanks,dude,your solution really does work..but I still have no idea why I got such a problem.At first,my server code was like this:DataInputStream is = new DataInputStream(socket.getInputStream()); byte[] b = new byte[1024]; is.read(b); GameRequest.parseFrom(b); and When I change the code like above, it just broke down... –  Ryan Zhu Jan 11 '13 at 7:15
@RyanZhu: Never call InputStream.read(byte[]) and ignore the return value. That may well have only read part of the data. Even if it didn't read just part of the data, you'd end up with a byte array which is then longer than expected, with trailing zeroes which would mess things up. Basically, there's a very good reason that Protocol Buffers supplies a method to read it from a stream - you might as well use that! –  Jon Skeet Jan 11 '13 at 12:10
The Java version of Protocol Buffers supports writing multiple Protocol Buffers to the same file (with a Length Delimiter before each Protocol Buffer). parseDelimitedFrom is for reading these Length-Delimited files. –  Bruce Martin Jan 13 '13 at 12:53
@BruceMartin: Yes, I'm aware of that - but as the OP didn't use writeDelimitedTo, using parseDelimitedFrom is going to fail. –  Jon Skeet Jan 13 '13 at 13:14
add comment

Your Answer


