Question

The problem is solved, see below...

I am currently developing a server-client application.

The server is programmed in C/C++ using openssl and the client is programmed in android-java using the SSLSocket class.
To write to the SSL-socket (client) I am using the BufferefWriter. I guess that is the main problem, but my research for my problem or for alternatives was not of success...

This is how I write data to the socket:

BufferedWriter out = new BufferedWriter(new PrintWriter(socket.getOutputStream(), true));
// This was a typo char[] data = new byte[] {0,0,0,255};
char[] data = new char[] {0,0,0,255};
out.write(data);

Now the server recieves 0,0,195,191
And that really confuses me!

But I guess it is due to the way the BufferedWriter works.

So what can I do in order to send binary data to the SSL socket?
What alternatives are there to the BufferedWriter?

Thanks for your time!

SOLVED

Ok thanks to McDowell and robermann I found the following sollution:

BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
byte[] data = new byte[] {0,0,0,255};
out.write(data);

I didn't know about the way java works with chars and thus was kind of confused.
For further information about the way chars are used in java take a look at McDowell's answer below.

Thanks to all you guys! :D

No correct solution

OTHER TIPS

char[] data = new char[] {0,0,0,255};

char values are implicitly UTF-16BE. The default encoding on Android is UTF-8. For the given constructor the PrintWriter will perform a transcoding operation from UTF-16 to UTF-8.

The above values should become the 5-byte sequence 00 00 00 c3 bf which (bar a leading byte) you are seeing on the server.

Types like DataOutputStream or something more formal (like Google Protocol Buffers) may be more suitable for low-level binary protocols.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top