Not sending XML command with header via TCP/IP using Java

0

I'm new to Java (basically had to learn it on the fly for this project), but I'm trying to send an XML command to a server to get some data from it. To do this, I've written a Java program and I'm running it from the command line.

Here's my Java code for a reference. I got most of this from a client/server TCP tutorial and adjusted the IP, port, and outgoing message accordingly. Again, I'm very new to this, so any help is appreciated.

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

public class GDC {
    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(<SERVER_IP>, <SERVER_PORT>);
        System.out.println("Socket Connected");

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

        // Initializing request content
        byte[] request = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><command version=\"2.2\" cmd=\"HEARTBEAT\"/>".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) 0x060E2B34;
        header[1] = (byte) 0x02050101;
        header[2] = (byte) 0x0F150111;
        header[3] = (byte) 0x00000000;
        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);
    }
}

I want to send XML request using the following instruction :

Screenshot Instructions

I got an error like follow:

Attempting connection to GE Reuter Stokes
Socket Connected
Sending request to Socket Server
Request was sent. Awaiting response.
Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
        at java.io.DataInputStream.readFully(DataInputStream.java:195)
        at java.io.DataInputStream.readFully(DataInputStream.java:169)
        at GDC.main(GDC.java:54)
java
xml
sockets
tcp
request
asked on Stack Overflow Oct 17, 2019 by Sanjay Vekariya • edited Oct 18, 2019 by Sanjay Vekariya

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0