[protobuf] Re: Using protocol buffers with sockets
send(connectedsocket,caBuffer,sizeof(saSbuf),0); -> send(connectedsocket,caBuffer, msg.ByteSize(),0); On 10月1日, 上午5时22分, EJ wrote: > I'm using the winsock api to send and receive protocol messages > between a client program and a server program. One program sends out a > periodic status message received by the second program, while the > second program sends a command message and then receives whatever was > sent to it. The problem isn't with the sending and receiving process, > but with decoding the protocol buffer message. When the command > message is sent, it appears to send and encode correctly, but when the > message is received by the other program, it's supposed to print out > the message contents, but just prints garbage. I'll put code similar > to mine here so that it makes more sense. I had to change the names > though, so hopefully it still makes sense. > This is a snippet so you can see the general idea: > > Sending program: > MyMessageNamespace::MyMessage msg; > char caBuffer[64]; > MyMessageNamespace::MyMessage::commandmsg * cmd= > msg.mutable_command(); > cmd->set_command("exit"); > int length = msg.ByteSize(); > if (msg.SerializeToArray(caSbuf,sizeof(caSbuf))) > { > send(connectedsocket,caBuffer,sizeof(saSbuf),0); > > } > > Receiving program: > MyMessageNamespace::MyMessage msg; > //other things, some of which involve system event code having to do > with socket messages. > //... > //finished with other things > if (WaitForSingleObject(Mymessageinfo->hmutex, 500) == WAIT_OBJECT_0) > { > msg.ParseFromArray(Mymessageinfo->mutexmessage, Mymessageinfo->msglength) > > //other things happen here > printf("%s",msg.cmd().command().c_str()); > > } > > Any ideas or does this not even make any sense? In case you're > wondering, yes we are basically using two different protocols here, > which I'm sure isn't helping the problem. > In any case, when I throw in code breaks, it looks like the problem > occurs when receiving program tries to decode the message. It could be > the message itself, but I think it has to do with the fact that I'm > new to protocol buffers and don't entirely understand all the things I > could be doing wrong here. But any help would be much appreciated! -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to protobuf@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
[protobuf] Re: Using protocol buffers with sockets
Did you remember to delimit your protobuf messages when sending them over your sockets? Your use of send() suggests you're using a stream socket and not a datagram socket (though that's no guarantee). If you tell Protobuf to decode starting from the wrong place, or don't give it a full message, or tell it to decode too many bytes, it will attempt to do any of the above and strange things may result. Chris On Sep 30, 2:22 pm, EJ wrote: > I'm using the winsock api to send and receive protocol messages > between a client program and a server program. One program sends out a > periodic status message received by the second program, while the > second program sends a command message and then receives whatever was > sent to it. The problem isn't with the sending and receiving process, > but with decoding the protocol buffer message. When the command > message is sent, it appears to send and encode correctly, but when the > message is received by the other program, it's supposed to print out > the message contents, but just prints garbage. I'll put code similar > to mine here so that it makes more sense. I had to change the names > though, so hopefully it still makes sense. > This is a snippet so you can see the general idea: > > Sending program: > MyMessageNamespace::MyMessage msg; > char caBuffer[64]; > MyMessageNamespace::MyMessage::commandmsg * cmd= > msg.mutable_command(); > cmd->set_command("exit"); > int length = msg.ByteSize(); > if (msg.SerializeToArray(caSbuf,sizeof(caSbuf))) > { > send(connectedsocket,caBuffer,sizeof(saSbuf),0); > > } > > Receiving program: > MyMessageNamespace::MyMessage msg; > //other things, some of which involve system event code having to do > with socket messages. > //... > //finished with other things > if (WaitForSingleObject(Mymessageinfo->hmutex, 500) == WAIT_OBJECT_0) > { > msg.ParseFromArray(Mymessageinfo->mutexmessage, Mymessageinfo->msglength) > > //other things happen here > printf("%s",msg.cmd().command().c_str()); > > } > > Any ideas or does this not even make any sense? In case you're > wondering, yes we are basically using two different protocols here, > which I'm sure isn't helping the problem. > In any case, when I throw in code breaks, it looks like the problem > occurs when receiving program tries to decode the message. It could be > the message itself, but I think it has to do with the fact that I'm > new to protocol buffers and don't entirely understand all the things I > could be doing wrong here. But any help would be much appreciated! -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to protobuf@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.