• Post Reply Bookmark Topic Watch Topic
  • New Topic

socket encodeing problem  RSS feed

zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am now writing a program to BE connected by a bw4.2 application by using socket,it means that my program open the server port,the bw call my socket and send me a message that is XML format,the socket component of bw is configured to use the "utf-16le",my program run on a chinese version windoes2003 server,that use the "iso8859-1" or "gb2312" encoding.the code snippet of my program as following:

ServerSocket server = new ServerSocket(10001);
Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
String inputLine = "";

while ((inputLine = in.readLine()) != null) {
String str=new String(inputLine.getBytes("iso8859-1"),"utf-16");

and the bw send following message to my program:



<TimeStamp>20/05/2004 13:58:34</TimeStamp>




but in my program,the the following is printed?



<TimeStamp>20/05/2004 13:58:34</TimeStamp>?



as you can see,in the end of each line,ths sign "?" is added,and the tag "</IncStatus>" is missed.

who can help me,how can i deal with the encoding problem?
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me state what I think you mean:
The server code page is "iso8859-1" but the client is sending bytes that were encoded from "utf-16le".

"new String(inputLine.getBytes("iso8859-1"),"utf-16");"

This won't do the trick. Here we are saying "encode inputLine into "iso8859-1" and then creates a new String from bytes that were encoded from "utf-16", which is not the case. That is, this technique cannot be used to convert between different encodings.

Try using "new BufferedReader(new InputStreamReader(
client.getInputStream(), "UTF16-LE"));"
this will translate the bytes received from the net --that were encoded from "utf16-le"-- into the 16 bits chars used in Java strings. The charset "iso8859-1" won't be used unless the information in the server is converted to bytes for writing to a socket or to a disk.

I hope it helps.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!