0

I want to communicate as a TCP Server on Port 2000 and 2001 with my TCP Client (Machine which sends Bytestreams). Therefore I programmed a Spring Boot Application in Java.

This Question is only for Port 2001: I use Camunda as BPMN-Engine for executing and orchestrating. I start Threads like this:

package com.example.workflow;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class StartTCPServersDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        Runnable serverZyklisch = new ServerZyklisch();
        Runnable serverAzyklisch = new ServerAzyklisch((String) delegateExecution.getVariable("param"));
        Thread t1 = new Thread(serverZyklisch);
        t1.start();
        System.out.println("Thread Zyklisch gestartet");
        Thread t2 = new Thread(serverAzyklisch);
        t2.start();
        System.out.println("Thread Azyk. gestartet");
        String val1 = (String) delegateExecution.getVariable("param");
        int valueParam = Integer.parseInt(val1);
        System.out.println("Param ist: "+valueParam);

    }
}

This is my ServerAzyklisch Class:

public class ServerAzyklisch implements Runnable, JavaDelegate {
    private ServerSocket ssocket;
    String param;
    HexToByteConverter hexToByteConverter = new HexToByteConverter();
    public ServerAzyklisch(String Pparam) throws IOException {
        ssocket = new ServerSocket(2000);
        param = Pparam;
    }

    public void run() {
        System.out.println(param+"Paraaam");
        InputStream in;
        OutputStream out = null;
        Socket socket;
        while(true){
            try {
                socket = ssocket.accept();
                in = socket.getInputStream();
                out = socket.getOutputStream();
                byte []data = new byte[132];
                int numBytes = 0;
                byte[]durch = hexToByteConverter.hexStringToByteArray("333333330041006400040000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                byte[]durchlauf = hexToByteConverter.hexStringToByteArray("333333330041006400040000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                byte[]Pressen1hexdump111 = hexToByteConverter.hexStringToByteArray("33333333003d0064000600000004004001c9c78900010000006f00000000000000000000000000010000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005");
                byte[]Pressen1hexdump110 = hexToByteConverter.hexStringToByteArray("33333333003d0064000600000004004001c9c78900010000006e0000000000000000000000000001000000000014000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"+param);
                byte[]Pressen2hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                byte[]Pressen3hexdump = hexToByteConverter.hexStringToByteArray("3333333300400065001400000000003d01c9c7890001000000c9000000000000000000000000000100000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                byte[]Pressen3hexdumpNextBohrer = hexToByteConverter.hexStringToByteArray("3333333300400065001400000000003f01c9c789000100000078000000000000000000000000000100000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002");
                byte[]Pressen4hexdumpNextRSCIDBohrer = hexToByteConverter.hexStringToByteArray("33333333003f0065001400000000003d01c9c78900010000007a000000000000000000000000000100000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                //gleichen Stream zurückschicken, der angekommen ist, für Durchlauf
                while((numBytes = in.read(data)) != -1){
                    System.out.println(Arrays.toString(data));
                    out.write(Pressen1hexdump110);
                    out.write(Pressen2hexdump);
                    out.write(Pressen3hexdumpNextBohrer);
                    //out.write(durchlauf);
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void execute(DelegateExecution delegateExecution) throws IOException {
          }
}

I get everytime a different Result to my Client, so the behaviour is always another. But I want to send once all three bytearrays to my Client. I think something is wrong with my while loop. Do you have any idea ?

7
  • Why did you put "connection keepalive" in the title, there's no mention of it in the question? "I get everytime a different Result to my Client" - You're always sending the same thing. If the client prints out different things, that's on the client side. Can you show the code for the client? Commented Aug 10, 2020 at 14:07
  • the client is a machine, which sends a bytestream. I dont have code for that. I want to read it the Clients Message once and then send the client back three different messages (bytestreams) but I think the while loop is wrong or something, so the Client won't get my three messages one after the other ? Maybe its because I only have while loop while the client is sending != -1, isnt there something like while(connection is keepalive), Then -> Send the 3 messages -> then End Commented Aug 10, 2020 at 14:11
  • the machines protocol is TCP only Commented Aug 10, 2020 at 14:13
  • If the problem that the "messages" you send from the server get mixed together, or that you send more or fewer of them than the client expects? Commented Aug 10, 2020 at 14:21
  • I think that the client expects always one Message. Normally the communication is like a three way communication. It starts: Client sends message to Server -> Server answers to client with one message -> Client asks server -> Server answers -> Client send message -> Server answers -> "End of communication" Commented Aug 10, 2020 at 14:24

1 Answer 1

1

By the comments, the communication is based on request-response pairs. You need to read 3 messages from the client, and return a response for each message. To do this, replace the while loop with:

            readMessage(in, data);
            out.write(Pressen1hexdump110);

            readMessage(in, data);
            out.write(Pressen2hexdump);

            readMessage(in, data);
            out.write(Pressen3hexdumpNextBohrer);

where the readMessage method is a new method you must add, that reads a complete request from the client.

If the client requests are always 128 bytes, there is a convenient method in DataInputStream that you can use:

void readMessage(InputStream in, byte[] buffer) throws IOException {
    new DataInputStream(in).readFully(buffer, 0, 128);
}

In the general case the readMessage method would have to look something like this in pseudo-code:

void readMessage(InputStream in, byte[] buffer) {
    // Read a message
    while message is not complete:
        read from "in" into "buffer"
        if "in" was closed: throw an exception because the connection was closed mid-request
        else: incorporate newly read data from "buffer" in message
    done
}
Sign up to request clarification or add additional context in comments.

5 Comments

The Message is a bytestream/bytearray which looks like: byte []bytesPressen = {51, 51, 51, 2, 0, 64, 0, 100, 0, 6, 0, 0, 0, 0, 0, 0, 0, -68, 97, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; its bytearray 128 length. can you show example java code, when message is completed if 128bytesend
Now I get following Error: java.io.EOFException at java.base/java.io.DataInputStream.readFully(DataInputStream.java:202) at com.example.workflow.ServerAzyklisch.readMessage(ServerAzyklisch.java:90) at com.example.workflow.ServerAzyklisch.run(ServerAzyklisch.java:71) at java.base/java.lang.Thread.run(Thread.java:834) Do you have any idea maybe ? Im also not 100% sure if the clients sends always 128 Bytes.
That exception tells you that the client closed its end of the connection without sending 128 bytes.
Do you have any idea ? Is there a way to get the length of the bytes, the client have sent ? Or how would be a way to solve this issue ?
TCP is a "stream" protocol - there is no built-in way to tell where one message ends and the next one begins. The messages don't have a built-in "length" field. This is a very common problem and also the reason why I asked if UDP or another protocol was a possibility. The documentation for the communication protocol your device is using should tell you how to read messages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.