Reputation: 59
Chrome extension use native messaging to receive json data:
var port = chrome.runtime.connectNative('company.my_application');
port.onMessage.addListener(function(result) {
console.log(result);
});
In my C++ program, if I send a json string str1[]
, extension can receive it, but if I send str2[]
, extension can not receive it.
Is there any problem with this string?
str2[]
stands for a long string which is a correct Json data, but can't be accepted by extension.
Did any one run into the same problem and resolve it ?
char str1[]="{\"Func\":\"test\",\"msg\":\"hello\"}";
char str2[]="{\"Func\":\"test\",\"msg\":\"3c476c6f62616c4a53206d6f64756c6549643d226f7261636c652e6f6174732e736372697074696e672e6d6f64756c65732e616466223e3c215b43444154415b2f2a2061616161616161616161616161616161612061616161616161612061616161616161616161206161616161202a2f262378443b262378413b262378443b262378413b616161616161616120616161616161616161616161616161616161616161616161616161612861612c6161616161297b262378443b262378413b096161617b262378443b262378413b090961616120616161613d61616161616161286161293b262378443b262378413b09096161616161203d206161616161616161286161616161293b262378443b262378413b0909616128276161616161273d3d6161616161616161616161616161616161616161616161616161616161612861612c27616161616161616161616161616161616120616161616161616161616161616161616161616161616161616161612729297b262378443b262378413b09090961616120616161616161616161613d2861616161613d3d616161613f27273a6161616161293b262378443b262378413b09090961616120616161613d616161612e6161616161616128293b262378443b262378413b09090961616120616161616161616161616161203d20616161612e5f6161616161616161616161616161612861616161293b262378443b262378413b090909616120286161616161616161616161612e61616161612e616161616161613d3d22616161616122297b262378443b262378413b090909096161616161616161616161612e616161616128293b262378443b262378413b0909097d262378443b262378413b090909616161612e5f6161616161616161616161616161616161612861616161292e6161616161203d20616161616161616161613b262378443b262378413b090909616161612e5f616161616161616161616161616161616161616161286161616161616161616161612c2061616161616161616161293b262378443b262378413b0909096161616161616161616161612e6161616128293b262378443b262378413b09097d616161617b262378443b262378413b0909096161616161616161616161616161616161612e6161612827616161616161616161616161616161616120616161616161616161616161616161616161616161616161616161612061616161616161616120616120616161612061612061616161616161612061612061616161616161612e2e2e2e61613a272b61612c6161616161616161616161616161616161612e61616161615f6161616161293b262378443b262378413b09097d0909262378443b262378413b202020207d616161616128616161297b262378443b262378413b20202020096161616161616161616161616161616161612e6161612827616161616161616161616161616161616120616161616161616161616161616161616161616161616161616161612061616161613a272b6161612e61616161616161616161612b272e2e2e2e61613a272b61612c6161616161616161616161616161616161612e61616161615f6161616161293b262378443b262378413b202020207d262378443b262378413b7d262378443b262378413b262378443b262378413b2f2a206161616161616161616161616161616161206161616161616161206161616161616161616120616161202a2f262378443b262378413b5d5d3e3c2f476c6f62616c4a533e\"}";
I'm trying to send the length and message as follows:
size_t len = strlen(str2);
cout.write(str2, len);
fflush(stdout);
I have use these ways to send length:
cout.write((char*)&len, 4);
putchar ((( char *) &len) [0]);
putchar ((( char *) &len) [1]);
putchar ((( char *) &len) [2]);
putchar ((( char *) &len) [3]);
putchar(char(((len >> 0) & 0xFF)));
putchar(char(((len >> 8) & 0xFF)));
putchar(char(((len >> 16) & 0xFF)));
putchar(char(((len >> 24) & 0xFF)));
Upvotes: 1
Views: 200
Reputation: 3688
[OP's solution converted to answer below]
Thanks everyone for comments, I have now fixed this problem.
Cause:
The length of str[]
is 2601(0x29 0x0a 0x00 0x00)
. My IO will automatically insert Ox0d
before 0x0a
, so my ouput length become 5 bytes, (0x29 0x0d 0x0a 0x00 0x00)
.
Solution:
Change IO mode.
#include<io.h>
#include<fcntl.h>
_setmode(fileno(stdout), O_BINARY);
Upvotes: 1
Reputation: 348992
You should use
unsigned int len = strlen(str2);
instead of
size_t len = strlen(str2);
The native messaging protocol requires that the message is preceded by a 32-bit integer, whereas the bit size of size_t
is implementation-dependent. On my 64-bit system, sizeof(size_t)
is 8 (bytes) for example.
Upvotes: 1