Skip to content
Advertisement

Unicode text through socket in java

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.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement