Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to byte array and vice versa after having sent data over sockets

I've got a Client and a Server, both running on the same Computer (with Windows 7 OS). Programming language is java.

to send a message I use:

public static void sendMessage(Socket socket,String message) throws IOException{
    byte[] mBytes = message.getBytes();
    output.write(mBytes, 0, mBytes.length);
    output.flush();
}

and to receive I use:

 byte read = (byte) input.read();

several times (the method is quite long, but the details I think arent that important) and store the result in a byte[] messageBytes. To retain the message I use

String message = new String(messageBytes);

This worked for me quite well. But I am wondering about the encodings used here. Will it be ok to use getBytes() on the one host and new String(byte[] bytes) on the other host? Is it good style to act like that?

like image 875
Hansi Zimmrer Avatar asked Feb 20 '26 02:02

Hansi Zimmrer


1 Answers

No, it is not ok without specifying the same encoding on conversion from and to bytes. This is because Java will use the default encoding, and those may be different on different servers.

So, say on the sender side you have default cp1252 (i.e. Windows host), but on the server site you have UTF-8 (Linux). Then you'll get garbage as soon as you have non-ASCII characters.

This is maybe hard to detect. For one, all your servers could have the same default encoding (like, if you run the client and the server on the same host). Second, make sure your test data contain some non-ASCII characters, like the € sign, Umlauts or even chinese characters.

Remember, even if presently client/server do run on the same host: Do it robust and correct from the beginning, so as to remove the restriction that your SW will only run correctly when client and server are on similar platforms.

like image 101
Ingo Avatar answered Feb 21 '26 14:02

Ingo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!