2016-12-15 1 views
1

안녕하세요.Google Protobuf - C++과 Python 간의 UDP 통신 - google.protobuf.message.DecodeError : unpack에는 길이 4의 문자열 인수가 필요합니다.

CPP의 한 프레임 워크와 파이썬의 다른 프레임 워크간에 메시지를 보내려고합니다. ,

package prototest; 

message DiceData { 
    required float gyrox = 1; 
    required float gyroy = 2; 
    required float gyroz = 3; 
    required float accelx = 4; 
    required float accely = 5; 
    required float accelz = 6; 
    required float roll = 7; 
    required float pitch = 8; 
    required float yaw = 9; 
    required int32 side = 10; 
    required float certainty = 11; 
    required string time = 12; 
} 

내가 통신이 작동 알고 이 Serialize C++ object to send via sockets to Python - best approach?

파이썬에서 내 서버 코드는 다음과 같습니다 :

import socket 
from DiceData_pb2 import DiceData 

UDP_PORT=1555 

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
sock.bind(("", UDP_PORT)) 

dicedata = DiceData() 
while True: 
    data, addr = sock.recvfrom(1024) 
    print data 
    dicedata.ParseFromString(data) 
    print ("gyrox = {0}".format(dicedata.gyrox)) 
    print("gyroy = {0}".format(dicedata.gyrox)) 
    print("gyroz = {0}".format(dicedata.gyroz)) 
    print("accelx = {0}".format(dicedata.accelx)) 
    print("accely = {0}".format(dicedata.accely)) 
    print("accelz = {0}".format(dicedata.accelz)) 
    print("roll = {0}".format(dicedata.roll)) 
    print("pitch = {0}".format(dicedata.pitch)) 
    print("yaw = {0}".format(dicedata.yaw)) 
    print("side = {0}".format(dicedata.side)) 
    print("certainty = {0}".format(dicedata.certainty)) 
    print("time = {0}".format(dicedata.time)) 

.proto 파일은 다음과 같다 나는에 표시된 동일한 프로세스를 따라 서버가 첫 번째 메시지를 수신하여이를 쓰레기로 인쇄하기 때문입니다. 그것은 ParseFromString 선에 도달 한 후에 그러나, 다음과 같은 오류가 발생합니다

Traceback (most recent call last): 
    File "server.py", line 13, in <module> 
    dicedata.ParseFromString(data) 
    File "/usr/local/lib/python2.7/dist-packages/google/protobuf/message.py", line 185, in ParseFromString 
    self.MergeFromString(serialized) 
    File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1095, in MergeFromString 
    raise message_mod.DecodeError(e) 
google.protobuf.message.DecodeError: unpack requires a string argument of length 4 

어떻게이 사람이 해결할 수 아는합니까

? 이전 줄에 쓰레기가 인쇄되어 있기 때문에 문자열이 비어 있지는 않지만 데이터 구조로 문자열을 다시 변환 할 수없는 것 같습니다.

답변

3

링크 된 질문의 C++ 코드가 손상되었습니다. 여기에는 다음 줄이 포함됩니다.

sendto(sock, buf.data(), strlen(buf.c_str()), 0, (struct sockaddr *)&addr, sizeof(addr)); 

잘못된 것입니다! 첫 번째 0 값 바이트에서 메시지를 잘라냅니다. 그것은이 대신과 같아야합니다

sendto(sock, buf.data(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr)); 

이 확실히 당신이보고있는 오류가 발생합니다.

이 수정 사항을 추가하는 다른 질문을 편집했습니다.