[SOLVED] Network library: issues with clientEvent

I have the following code inside my program (the entire sketch is quite large). Inside the ClientEvent function i ask if the received input is null. However, when i run the code i get the following output in my console:

SUCCESS now connected to: [my ip]: 2709
Your IP: [my ip]
inString: null

So c.readString is null. Even though I explicitly asked before. I am very confused and hope that somebody has an idea what this could mean.

void connectToServer() {
  String address = JOptionPane.showInputDialog("Enter IP and Port \n(format: 192.168.1.111:1234)");
  String a[] = address.split(":");
  SERVER_PORT = Integer.parseInt(a[1]);
  SERVER_IP = a[0];
  c = new Client(this, SERVER_IP, SERVER_PORT);
  MY_IP = c.ip();
  println("SUCCESS now connected to: " + SERVER_IP + " port: " + SERVER_PORT + "\nYour IP: " + MY_IP);
}



void clientEvent(Client c) {
  if (c.readString() != null) {
    println("inString: " + c.readString());
    nch.getData(c.readString()); //nch is my custom input handler class, shouldn't matter here
    c.clear();
  }
}
1 Like

Please format your code. Edit your post, select your code and hit the </> button.

I believe every time you call c.readString(), you are retrieving new data from your connection. The first read retrieves the data. If there is no more data in the queu, then readString() would be null.

Kf

void clientEvent(Client c) {
  String inread=c.readString();
  if ( != null) {
    println("inString: " + inread);
    nch.getData(inread); //nch is my custom input handler class, shouldn’t matter here
    c.clear();
  }
}
2 Likes

Ok it works when I save the input in a variable the first time i read it. But what exactly is the purpose of clear() then when the inputs are no longer available after reading?

Calling c.clear() removes any more strings that might be waiting in the buffer. That is, if whatever is generating these strings is doing so at a faster rate that you are processing them, they’ll wait their turn in a buffer so that readStrings() will still get all the strings in the order they were generated. Calling the clear() function wipes out that buffer.

It’s like managing a restaurant with only one seat. No one is waiting and you try to fill the seat, you get nobody (null). If one person is waiting, they get the seat, and then nobody is waiting. But if a bunch of people show up, there’s a line to eat. You can process them one at a time in order with more calls to readStrings(), or you can tell them all to take a hike with clear().

https://processing.org/reference/libraries/net/Client_clear_.html

2 Likes

If you want to process all the data in the buffer, then instead of calling clear, you will do something like

String inread=c.readString();;
while(inread!=null){

   ....
   ....  Process what is in inread
   ....

   inread=c.readString();  //Last line
}

as Thomas mentioned in his post, this is necessary only if the input rate is faster than Processing’s processing rate. This is usually when your incoming data has a higher rate than 60Hz. Take this as an approximation as there are some other considerations…

Kf

1 Like