OK, Thanks. Let me post my code here a little bit:
message MatrixMsg
{
required string msgType = 1;
optional int64 count = 2;
optional string extraInfo = 3;
message TaskMsg
{
required string taskId = 1;
required string user = 2;
required string dir = 3;
required string cmd = 4;
required int64 dataLength = 5;
}
repeated TaskMsg tasks = 4;
}
In my code, at some place, I did something like the following:
MatrixMsg mm;
mm.set_msgtype("client send task");
mm.set_count(100);
for (int i = 0; i < 100; i++)
{
MatrixMsg_TaskMsg *tm = mm.add_tasks();
tm->set_taskid("someid");
tm->set_user("kwang");
tm->set_dir("somedir");
tm->set_cmd("somecmd");
tm->set_datalength(0);
}
string mmStr = mm.SerializeAsString();
char *mmChar = new char[mmStr.length()];
for (int i = 0; i < mmStr.length(); i+=)
{
mmChar[i] = mmStr[i];
}
/*
create some socket "sockfd" here
*/
send(sockfd, mmChar, mmStr.length(), 0);
Now, at the sever side, I still received the message that got truncated.
Does that mean the server hasn't finished received the whole data in
"mmChar", do I need to do a loop receive until I get all the messages?
Thanks!
Ke
On Wednesday, March 12, 2014 8:56:32 AM UTC-5, Ilia Mirkin wrote:
>
> On Wed, Mar 12, 2014 at 9:48 AM, Ke Wang <[email protected] <javascript:>>
> wrote:
> > Thanks, but when I print out the char* using string.data(), it got
> truncated
> > too.
>
> Right... most print functions will stop when they see a null
> character... you can't use printf/cout/etc -- you'd have to write a
> custom print mechanism that e.g. printed out hex. For example
>
> void print_data(const std::string& str) {
> for (int i = 0; i < str.length(); i++) {
> if (i && (i % 20) == 0) printf("\n");
> printf("%02X ", str[i]);
> }
> if (i % 20) printf("\n");
> }
>
> Or something along those lines. (I don't actually remember if str[i]
> works, but if not, should be easy to do e.g. str.data()[i] or
> something.)
>
> > Later, I need to transfer this char* through TCP socket, which
> > apparently cannot be decoded right at the server side. Did I do
> something
> > wrong with the definition of the message?
>
> Check what data is sent over the socket with e.g. tcpdump and make
> sure that it's the data you expect. Make sure that if you're never
> handling the string as a char*. It needs to always be a (char *,
> length) pair (which is essentially what std::string is). Otherwise the
> implicit termination of c-style strings (0 byte) will break things.
>
> -ilia
>
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.