socket encoding problem


General J2EE: socket encoding problem

  1. socket encoding problem (1 messages)

    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?
  2. socket encoding problem[ Go to top ]

    I think you should use an InputStreamReader where you can specify your charset:
    InputStreamReader(InputStream in, Charset cs)
    InputStreamReader(InputStream in, CharsetDecoder dec)
    InputStreamReader(InputStream in, String charsetName)

    Best regards, Mircea