Peter Paul - 2018-12-07

this is what i have, not sure if is the best

public void run() {

        byte[] lenbuf = new byte[2];

        try {
            while (this.ws_sock != null && this.ws_sock.isConnected()
                    && Thread.currentThread().isAlive()
                    && !Thread.currentThread().isInterrupted()) {

                if (this.ws_sock.getInputStream().read(lenbuf) == 2) {
                    int size = ((lenbuf[0] & 0xff) << 8) | (lenbuf[1] & 0xff);
                    byte[] buf = new byte[size];
                    if (ws_sock == null) {
                        ws_sock = ws_sock2;
                    }
                    // We're not expecting ETX in this case
                    this.ws_sock.getInputStream().read(buf);
                    if (buf.length > 2) {
                        //_tracePrinter.PrintScreenTrace((new String(buf)), "", 0);
                        System.out.println(new String(buf, "UTF-8"));
                        new ConnectionHandler(buf, this.ws_sock);
                    }
                }
                if (this.ws_sock == null && this.ws_sock.isConnected() == false) {
                    this.ws_sock.close();
                }
                Thread.sleep(300);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            this.ws_sock.close();
        } catch (IOException ex) {
            ex.getStackTrace();
        }
    }