简体   繁体   中英

openssl java to c++ read int issue

I am using c++ ssl socket server(32 bit linux) & java based ssl client(windows 64bit). SSL Handshake is sucess. When i send string data from Java client to C++ Server able to receive & display. When i send int data from Java client to c++ Server ,different data i am receiving

Java send source:

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
osw.writeInt(1000);
osw.flush();

C++ readInt source:

unsigned int result =0;
int byteRead = SSL_read(m_ssl, &result, sizeof(int));
unsigned int rxdInt = htonl(result);

c++ side ,when i print byte by byte for bytes read : 1

Expected: e8 03 00 00 
received : 00 00 00 00 
after htonl: 00 00 00 00

With below code ,writing as bytes ,its working with c++

ByteBuffer bb = ByteBuffer.allocate(4); 
bb.putInt(1000); 

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.write(bb.array());
dos.flush();

Without SSL writeInt is working with c++ with SSL writeInt not working with SSL ,writing int as bytes working. Why writeInt behavior is different in SSL case?

Why are you using a Writer for binary data? You should be using DataOutputStream.writeInt() .

And then of course this:

unsigned int rxdInt = htonl(result);

should be this:

unsigned int rxdInt = ntohl(result);

And you're ignoring the value returned by SSL_read() , which could have been -1, zero, or less than 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM