繁体   English   中英

通过TCP发送转换为字节数组的XML,然后将响应转换回可读XML的最佳方法是什么?

[英]What is the best way to send XML converted to a byte array over TCP, then translate the response back to readable XML?

我被要求编写一种解决方案,该解决方案将XML转换为字节数组,以作为请求通过纯TCP与该服务所需的其他参数一起通过.TCP服务发送给.Net服务。 该服务以字节数组格式返回XML。 在以下解决方案中,我尝试实现这些原则:

  1. 打开插座。
  2. 打开输入流,然后将输出流输出到套接字。
  3. 根据服务器协议从流中读取和写入流。
  4. 关闭流。
  5. 关闭插座。

请注意,我从一个主程序调用下面的代码,该程序将XML文件转换为requestData参数的字节数组,并关闭通过创建类的实例打开的套接字。 在输出中,我得到了一个看起来像[B@60076007

我的问题是:上面的输出示例是否需要转换回XML的字节数组格式?如果是,是否有关于如何执行此操作的建议? 有没有更好的方法来组织此代码? 像发送和接收应该在单独的功能? 感谢您抽出宝贵的时间愿意回答。

public class  TCPTest implements Closeable {


private static final Logger logger = Logger.getLogger( TCPTest.class);

private Socket socket;

public  TCPTest(String host, int port) throws IOException {
    this(new Socket(host, port));
}

public  TCPTest(Socket s) {
    this.socket = s;
}

public void send TCPRequest(int testId, String method, byte[] requestData, boolean sendLength) throws IOException {

    socket.setSoTimeout(40000);

    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream dis = new DataInputStream(socket.getInputStream());

    try 
    {
        dos.write(testId);
        dos.writeBytes(method);

        dos.write(requestData); // write the message
        System.out.println("length of request: " + requestData.length);

        if (sendLength) {
            String lengthDesc = "LEN*";
            byte[] lengthDescBytes = lengthDesc.getBytes("US-ASCII");

            dos.write(lengthDescBytes);

            dos.write(requestData.length);
        }

    } catch (UnknownHostException e) {
        System.err.println("Unable to find host: 3125");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: 3125");
    }

    try 
    {
        int lengthResponse = dis.read(); // get length of incoming message
        System.out.println("length of response: " + lengthResponse);

        while (lengthResponse > 0) {
            byte[] message = new byte[lengthResponse];
            System.out.println(message);

            dos.write(message, 0, lengthResponse);

        }

    } catch (EOFException e) {
        System.err.println("Input stream ended before all bytes read");
    } catch (IOException e) {
        System.err.println("I/O failed on the connection to: 3125");
    }

    dos.flush();
    dos.close();
}

@Override
public void close() throws IOException {
    this.socket.close();
}

}

因此,从根本上说,传入的数据是一个byte[]因为这是关于该对象[B@60076007 ,至少是[B部分byte[]字符串形式。

所以现在,由于XML只是简单的String您可以使用String(byte[] bytes, Charset charset)将字节数组转换为String String(byte[] bytes, Charset charset)其中您必须提供将用于转换的编码。

如果这还不够的话,可以从字符串形式创建您将需要的任何其他XML表示形式(w3c,dom4j等)。

除了查看您的代码外,我还可以看到您根本没有读取服务器输出。

int lengthResponse = dis.read(); // get length of incoming message

不,此行从流中读取第一个字节(不是整数)。 整数为4个字节的长度。

 byte[] message = new byte[lengthResponse];
    System.out.println(message);

您正在创建新的字节数组(缓冲区),这很不错,但是您永远不会对其进行写入(通过从输入流中读取)。 之后,您将打印message.toString()方法的结果。

您需要先阅读整个消息,然后再进行转换和存储。

伪代码:

    InputStream in;
    byte[] buff=new byte[your_length]

    in.read(buff) //this line will read your_length bytes from the input stream
    String message=new String(buff,ProperCharset); //convert to string
    sysout(message); // print it
    //do whatewer you like eg, store to your DataOutputStream with 
    // out.write(message.getBytes(ProperCharset));
    // or directly out.write(buff);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM