Skip to content
Advertisement

Java Socket Read Input Twice

I have a situation with a Java Socket Input reader. I am trying to develop an URCAP for Universal Robots and for this I need to use JAVA.

The situation is as follow: I connect to the Dashboard server through a socket on IP 127.0.0.1, and port 29999. After that the server send me a message “Connected: Universal Robots Dashboard Server”. The next step I send the command “play”. Here starts the problem. If I leave it like this everything works. If I want to read the reply from the server which is “Starting program” then everything is blocked.

I have tried the following:

-read straight from the input stream-no solution

-read from an buffered reader- no solution

-read into an byte array with an while loop-no solution

I have tried all of the solution presented here and again no solution for my case. I have tried even copying some code from the Socket Test application and again no solution. This is strange because as mentioned the Socket Test app is working with no issues.

Below is the link from the URCAP documentation:

https://www.universal-robots.com/articles/ur/dashboard-server-cb-series-port-29999/

I do not see any reason to post all the trials code because I have tried everything. Below is the last variant of code maybe someone has an idea where I try to read from 2 different buffered readers. The numbers 1,2,3 are there just so I can see in the terminal where the code blocks.

In conclusion the question is: How I can read from a JAVA socket 2 times? Thank you in advance!

public void sendPlay()  {
                        
    try {
        // Create a new Socket Client
        Socket sc = new Socket("127.0.0.1", 29999);
        
        if (sc.isConnected()) {
            
            InputStream is = sc.getInputStream();
            BufferedInputStream in = new BufferedInputStream(is);
            String data = "";
            int s = in.read();
            
               
            data += ""+(char)s;
            int len = in.available();
            System.out.println("Len got : "+len);
            if(len > 0) {
                byte[] byteData = new byte[len];
                in.read(byteData);
                data += new String(byteData);
            }
            
            System.out.println(data);
            System.out.println("1");
            
            
            
            // Create stream for data
            DataOutputStream out;
            out = new DataOutputStream(sc.getOutputStream());
            String command = new String();

            command = "play"+"n";
            // Send command
            out.write(command.getBytes("US-ASCII"));
            out.flush();
            System.out.println("2");
            
            
            InputStream is1 = sc.getInputStream();
            BufferedInputStream in1 = new BufferedInputStream(is1);
            String data1 = "";
            int s1 = in1.read();
            
               
            data1 += ""+(char)s1;
            int len1 = in1.available();
            System.out.println("Len got : "+len1);
            if(len1 > 0) {
                byte[] byteData1 = new byte[len1];
                in.read(byteData1);
                data1 += new String(byteData1);
            }
            
            System.out.println(data1);
            
        
            System.out.println("3");
            

            // Perform housekeeping
            out.close();
            sc.close();
        }
        sc.close();
    } catch (IOException e) {
        System.out.println(e);
    }
    
}

Advertisement

Answer

I have found a solution to the issue of reading the from the socket multiple times with a Swing GUI.

public void sendPlay() {
Thread appThread = new Thread() {
        public void run() {
            try {
                RobotTester robot = new RobotTester("127.0.0.1", 29999);
                    System.out.println("Connected to robot.");
                    robot.nextInput();               //Read and print robot's welcome message
                    robot.writeCommand("play");      //Send command
                    String resp = robot.nextInput(); //Read result
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Finished on " + Thread.currentThread());
        }
    };
    appThread.start();}

It seems that the background socket reading needs to be on a separate thread. This was causing the entire robot to be blocked. The idea was from an forum. It was not mine, but hey, it works.

Thank you very much!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement