Separating Net messages

edited April 2015 in Library Questions

Starting to use the Net library and notices that sometimes multiple messages would "stick" together and be read as one message. Ive started using a start and end flag and string indexOf to find complete messages out of the clusters. I was wondering if there is a built in or easier way to do this?

input "start""message1""end""start""message2""end"

output message1 message2

Answers

  • edited April 2015 Answer ✓
    • In terms of efficiency, 1 char value is faster than a whole String for searching.
    • And no need for both "start" & "end". 1 is pretty enough to define as delimiter signal.
    • I propose some unused char like \f as your DELIM at the end (or start) of every unit.
    • You can stick w/ indexOf() or use split() to get the message separated as String[]. :)

    https://processing.org/reference/split_.html
    https://processing.org/reference/String_indexOf_.html

    // forum.processing.org/two/discussion/10248/separating-net-messages
    
    static final char DELIM = '\f';
    static final String MSG = "Message" + DELIM + "received here" + DELIM + "and now!" + DELIM;
    
    void setup() {
      println("Batch received:\n" + MSG + '\n');
    
      String[] msgs = split(MSG, DELIM);
      println("# of tokens in this transmission batch:", msgs.length - 1, '\n');
      printArray(msgs);
    
      exit();
    }
    
Sign In or Register to comment.