socket encoding problem

Discussions

General J2EE: socket encoding problem

  1. socket encoding problem (1 messages)

    hello
    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(
    client.getInputStream()));
    String inputLine = "";

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



    and the bw send following message to my program:


    <IncStatus>

    <IncidentID>83700</IncidentID>

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

    <UserNameID>Bruce</UserNameID><WorkstationID>CLIENT1</WorkstationID>

    <StatusID>3</StatusID>

    </IncStatus>


    but in my program,the the following is printed?

    <IncStatus>?

    <IncidentID>83700</IncidentID>?

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

    <UserNameID>Bruce</UserNameID><WorkstationID>CLIENT1</WorkstationID>?

    <StatusID>3</StatusID>?



    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 ]

    Hi,
    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