Arabic (non western) characters over sockets
Hi All,
I need to send arabic (non western) characters over socket
I used PrintWriter
and BufferedReader
but the characters Appears as question marks ? after reaching the other side .
any one knows how to do it
a code snippets will be very helpful
thnx in advance
Message was edited by:
amgad
[357 byte] By [
amgada] at [2007-11-27 1:54:03]

# 1
It's likely an encoding mismatch or lack of encoding support.
PrintWriter uses the system's default charset.
BufferedWriter's used charset depends on the Reader used. If you're using InputStreamReader, the used charset is configurable, and defaults to the system's default charset if unset.
Possible causes for the problem:
1. A charset is configured in BufferedWriter, and it is not the same as the system's default on the computer using PrintWriter.
2. No charset is configured, but the two computers are set to different default charsets.
3. The charsets match, but it doesn't support Arabaic characters.
My best tip is to switch to Unicode on both sides. UTF-8 should give you good compression, and high compatibility. To create the InputStreamReader with that charset, simply specify "UTF-8" as charsetName.
As for the PrintWriter, instead of creating it directly from the OutputStream, create it from a BufferedWriter(or just use BufferedWriter directly). Setting the charset from this point is identical to the above.
You may also consider switching to byte-streams and doing the conversions manually. You'll have the added benefit of being able to log and debug the intermediate bytes and verify that the encoding is as desired. The down side is that it's a bit more work.
P.S. Note that I'm doing alot of guess-work on what you're using since you've posted no code. At a minimum, you should give a complete list of the classes you go through to get e.g. PrintWriter from Socket(as PrintWriter has no constructor taking Socket, and Socket has no member function returning PrintWriter).
# 2
It appears that my problem is in the default chracter encoding of the vm not in sending over socket because i tried to print from text box in the client side through keylistener but i got ? again
129: JTextField userTextField= new JTextField();
130: userTextField.addKeyListener(new KeyAdapter() {
131: public void keyTyped(final KeyEvent e) {
132:log.debug(e.getKeyChar());
133:log.debug(Charset.defaultCharset());
}
});
the result is
DEBUG [AWT-EventQueue-0] (LoginFrame.java:keyTyped:132) - ?
DEBUG [AWT-EventQueue-0] (LoginFrame.java:keyTyped:133) - windows-1252
DEBUG [AWT-EventQueue-0] (LoginFrame.java:keyTyped:132) - ?
DEBUG [AWT-EventQueue-0] (LoginFrame.java:keyTyped:133) - windows-1252
So
How to set chracter encoding to UTF-8 for my application since it's now windows-1252
# 3
> How to set chracter encoding to UTF-8 for my application since it's now windows-1252
I've already given a complete explanation in my post. But maybe it would be better understood if I use less words:
new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
I'm assuming you know how to use the BufferedReader and BufferedWriter. It's not that tough, really.
# 4
> It appears that my problem is in the default chracter
> encoding of the vm not in sending over socket ....
Perhaps this also would help...
Sockets send datat (bytes) they never ever send characters. Period.
So it has nothing to do with the socket but rather with the way you are getting information into and out of the socket.