没有收到使用Java通过TCP / IP发送XML的响应

时间:2016-08-04 19:36:30

标签: java xml sockets tcp

我是Java的新手(基本上必须动态学习这个项目),但我正在尝试向服务器(我实验室中的传感器)发送XML命令以从中获取一些数据。为此,我编写了一个Java程序,并从命令行运行它。连接建立成功,并且(我认为)消息正在成功发送 - 但是它已经卡住了“等待响应”。

这是我的Java代码供参考。我从客户端/服务器TCP教程中获得了大部分内容,并相应地调整了IP,端口和传出消息。再说一遍,我对此很新,所以任何帮助都表示赞赏。

// Java Socket Example - Client

import java.io.IOException; // Throws exception if there is an issue with input or output
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress; // This class represents an Internet Protocol address
import java.net.Socket;
import java.net.UnknownHostException;

/**
* This class implements java socket Client
*/

public class SocketClientExample {
	public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException {
		// get the localhostIP address, if server is running on some other IP, use that 
		System.out.println("Attempting connection to GE Reuter Stokes");
		InetAddress host = InetAddress.getByName("10.212.160.4"); // IP GOES HERE
		Socket socket = null; // start out as null, protocal
		ObjectOutputStream oos = null; // This will change, just setting default
		ObjectInputStream ois = null;
		// establish the socket connection to the server
		socket = new Socket("10.212.160.4", 3010); // 9876 is just the port number
		System.out.println("Made it past Socket Initialization");
		// write to socket using ObjectOutputStream
		oos = new ObjectOutputStream(socket.getOutputStream()); // new instance of OOS that will write to the socket
		System.out.println("Sending request to Socket Server"); 
		// Initializing request string
		String request = new String("0xF00041 " + "<Discovery><CommChannelName>Unknown</CommChannelName></Discovery>");
		// In our version, this is where the XML script would go
		oos.writeObject(request);
		System.out.println("Request was sent. Awaiting response.");
		// read the server response message
		ois = new ObjectInputStream(socket.getInputStream());
		// convert the response into a string
		String message = (String) ois.readObject();
		System.out.println("Message: " + message);
		// close your resources
		ois.close();
		oos.close();
		Thread.sleep(100);
	}
}

这很可能与传感器有关 - 但我认为在代码上留下第二眼也不会有什么坏处。

1 个答案:

答案 0 :(得分:1)

传感器期望XML前面有二进制5字节标头,但您要将标头作为 8个字符的十六进制编码字符串发送。< / p>

此外,您正在使用ObjectOutputStreamObjectInputStream,它们用于序列化Java对象,但您不是在发送/读取Java对象。所以这些是完全错误的流类使用。

因此,您的代码没有发送传感器所期望的内容,因此它无法正确接收您的请求,更不用说发送您可以收到的响应。

尝试更类似的东西(假设传感器以与请求类似的标头+ XML格式发回响应):

import java.io.IOException;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import jva.io.BufferedInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;

public class SocketClientExample {
    public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException {
        System.out.println("Attempting connection to GE Reuter Stokes");

        // establish the socket connection to the server
        // using the local IP address, if server is running on some other IP, use that 
        Socket socket = new Socket("10.212.160.4", 3010);
        System.out.println("Socket Connected");

        // write to socket using OutputStream
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

        // Initializing request content
        byte[] request = "<Discovery><CommChannelName>Unknown</CommChannelName></Discovery>".getBytes(StandardCharsets.UTF_8);

        // DataOutputStream.writeInt() writes in big endian and
        // DataInputStream.readInt() reads in big endian.
        // using a ByteBuffer to handle little endian instead.

        byte[] header = new byte[5];
        ByteBuffer buf = ByteBuffer.wrap(header, 1, 4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        // Initializing request header
        header[0] = (byte) 0xF0;
        buf.putInt(request.length);

        System.out.println("Sending request to Socket Server"); 

        // Sending request
        dos.write(header);
        dos.write(request);
        dos.flush();

        System.out.println("Request was sent. Awaiting response.");

        // read from socket using InputStream
        DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

        // Read response header
        dis.readFully(header);
        buf.flip();

        // Read response content
        byte[] response = new byte[buf.getInt()];
        dis.readFully(response);

        // convert the content into a string
        String message = new String(response, StandardCharsets.UTF_8);
        System.out.println("Message: " + message);

        // close your resources
        dis.close();
        dos.close();
        socket.close();

        Thread.sleep(100);
    }
}