On inspection, your code seems fine. (Why you bother to create a
mmChar instead of just passing mmStr.data(), mmStr.length() to send is
unclear to me, but I'm guessing you're trying to replicate the larger
codebase.) It's been long enough that I don't remember off-hand if
send() is guaranteed to send everything on a TCP socket (it definitely
isn't if you've set a E_NONBLOCK flag on the fd, or on a
non-SOCK_STREAM/SEQPACKET socket). Of course a signal to the process
would definitely make send return early though, so to be safe you need
a retry loop around it.

I would _really_ recommend looking at tcpdump output to make sure that
the full data is sent instead of speculating though. And also to make
the server end print the data received.

  -ilia

On Wed, Mar 12, 2014 at 10:20 AM, Ke Wang <kewang...@gmail.com> wrote:
> 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 <kewa...@gmail.com> 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 protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to