How to send by socket C++ compatible struct from Java? -
suppose there written, unchangeable host program, receives such c++ struct socket:
#pragma pack(push, 2) struct data { double x; double y; double z; long framenumber; }; #pragma pack(pop)
platform: c++ / 32-bit windows application compiled in visual studio 2008
how send such data java socket? have attemted fill bytebuffer putdouble(), , putlong(), putint() assuming long 32-bit, cannot produce valid values.
i have generated , sended data randomly, , structure-level bytes assignment looks fine(i can randomize 1 value, example x), cannot produce exact value (propably different double representation?), random stuff sending math.random() * double.max_value;
can use google protocol buffers on 1 side (client producing data) solve problem?
remember, cannot change server (receiving) side
i know can move sending data c++ , use jni, i'm searching simpler solution.
i see @ least 2 possible problems here:
- double representation @ byte-level (like 01234567 , 76543210 on other side)
- bits representation in 1 byte (like 00000001 , 1000000 on other side)
what things in java in c++?
in several hours provide exact sample data "byte-hacking" (exact java value before send , value of received double in c++)
about server bad-design answering
i know such implemented , unchangeable server bad software, environment in problem inject data small old application @ "hobbistic-level"
java representation
maybe bits-level c++ expert:
long.tobinarystring( double.doubletorawlongbits( (double) 0 ) ); // 000000000000000000000000000000000000000000000000000000000000000 long.tobinarystring( double.doubletorawlongbits( 1 ) ); // 011111111110000000000000000000000000000000000000000000000000000 long.tobinarystring( double.doubletorawlongbits( 1.1 ) ); // 011111111110001100110011001100110011001100110011001100110011010 long.tobinarystring( double.doubletorawlongbits( 1024 ) ); // 100000010010000000000000000000000000000000000000000000000000000 long.tobinarystring( double.doubletorawlongbits( 1024.1024 ) ); // 100000010010000000000000110100011011011100010111010110001110001 long.tobinarystring( double.doubletorawlongbits( double.max_value ) ); // 111111111101111111111111111111111111111111111111111111111111111
suppose there written, unchangeable host program, receives such c++ struct socket
then have major problem unknown person has kindly left legacy. have produce java code conform specific c++ compiler's conception of wire format of specific struct
, depends on:
- the hardware
- the compiler vendor
- the compiler version
- the compilation options
- the surrounding #pragmas
- ...
i strongly suggest you take opportunity fix whole megillah , define proper wire format transaction.
once you've done can emulate enough facilities of dataoutputstream
, or if problem worse , endian-ness involved, nio plus bytebuffer.
Comments
Post a Comment