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

问题描述:

我是Java的新手(基本上不得不为此项目动态学习它),但我试图发送一个XML命令到服务器(一个传感器在我的实验室)从它获得一些数据。为此,我编写了一个Java程序,并从命令行运行它。连接建立成功,并且(我认为)该消息正在成功发送 - 但它正陷入“等待响应”。没有收到回应通过TCP/IP使用Java发送XML

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

这是极有可能的东西与传感器多 - 但我想它不会伤害到对码的第二双眼睛。

+0

排除服务器侧:

尝试更多的东西这样代替(假定传感器发送回以类似的报头+ XML格式作为对该请求的响应)。为此创建一个测试服务器,输出它获取的数据和其他相关的调试消息。用结果编辑这个问题。为此使用'ServerSocket'。 Google了解ServerSocket文档。在创建一个新的ServerSocket之后,接受一个与'Socket client = server.accept();'的连接,然后从该客户端的输出流中读入数据,打印出来并回复。 – Aaron

+1

'ObjectOutputStream'和'ObjectInputStream'不是在这种情况下使用的正确类型的流,当然不是'writeObject()'和'readObject()'方法。你应该使用'OutputStreamWriter'(在其上有一个'BufferedWriter')或'DataOutputStream'和'InputStreamReader'(在它上面有一个'BufferedReader')。 –

+1

另外,0xF00041应该代表什么?你确实意识到你将它作为一个8字符的字符串发送,而不是3-4字节的整数,不是吗?传感器实际上期望您发送什么?你能提供一些协议文件吗? –

传感器预计由二进制5字节的报头前面的XML,但是要发送的报头作为8个字符的十六进制编码的串代替。

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

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

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); 
    } 
} 
+0

你是一个拯救生命的人!谢谢! –