问题发生在服务器端, parseDelimitedFrom() 调用。
客户端:
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();
服务器端:
try{
DataInputStream is = new DataInputStream(socket.getInputStream());
GameRequest gameRequest = GameRequest.parseDelimitedFrom(is);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
这是另一个类似的问题:
客户端:
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();
套接字处理是相同的。
服务器端:
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());
事实证明,服务器端将在 parseFrom() 再次调用时中断。但是当我评论两行时:
cardOnSell.setCurrentPrice(111);
cardOnSell.setFixedPrice(555);
在客户端,parseFrom() 调用正常工作。起初我怀疑.proto文件有问题,并且已经证明那里没有问题......那么这个问题是怎么来的?是因为我在调用 parseFrom() 之前错过了一些数据吗?