I am facing a tiny issue (I believe) in socket programming. When sending text from non-English languages, I get garbled results. After a lot of researching on google, I made some corrections. I changed getBytes()
to getBytes("UTF-8")
and tried to send some Arabic text.
When connecting sockets locally, it works fine. I see the arabic text I expected. But when testing from online, the results display strange/garbled characters.
Here is the text I tried:
“مرحبا” (this is the arab text of “hello”) which displayed to me as “مرØبا”
Please help me in resolving this issue.
Advertisement
Answer
This is some Java code I had lying around that’s used for setting the stream encodings on a pair of byte streams, but you could do the same with a singleton, at least assuming you’re using TCP stream sockets not UDP datagrams.
Process
slave_process = Runtime.getRuntime().exec("cmdname -opts cmdargs");
OutputStream
__bytes_into_his_stdin = slave_process.getOutputStream();
OutputStreamWriter
chars_into_his_stdin = new OutputStreamWriter(
__bytes_into_his_stdin,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newEncoder()
);
InputStream
__bytes_from_his_stdout = slave_process.getInputStream();
InputStreamReader
chars_from_his_stdout = new InputStreamReader(
__bytes_from_his_stdout,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()
);
InputStream
__bytes_from_his_stderr = slave_process.getErrorStream();
InputStreamReader
chars_from_his_stderr = new InputStreamReader(
__bytes_from_his_stderr,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()
);