Processing Network Client.available();

Hi Guys,
I work with the processing library and would like to know if there are any servers before I write. The Problem: client.available doesn’t work before writing:

Server:

import processing.net.*;
Server myServer;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204); 
}

void draw() {
  Client c = myServer.available();
  if(c != null)
  background(c.read());
}

Client:

import processing.net.*; 

Client myClient; 
int dataIn; 
 int val = 0;
void setup() { 
  size(200, 200); 
  // Connect to the local machine at port 5204.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 5204); 
} 
 
void draw() { 
  val = (val + 1) % 255;

  print(myClient.available());
  myClient.write(val);
  background(val); 
}

Hi @Moritz ,

I believe that the problem is on your Client IP code:
myClient = new Client(this, "127.0.0.1", 5204);
Check your computer IP to match the network you are using or search what is my ip on Google and try substituing there.

Also, for future research you might find helpful the following links:




He provides a lot of information that I found useful myself.

Best regards

1 Like

Hi,

Actually you don’t need to know your global IP because "127.0.0.1" is referring to the loopback adress (localhost) on your computer.

Your example works fine on my computer, the background is effectively changing so i didn’t understand what is the problem.

1 Like

I want to know if a server is available or not before i write to it as a client. It works before loading but not before sending. If the client is receiving information from the server it works, but not if the client is sending information.

Example (cleint receives Information):


import processing.net.*;

Server myServer;
int val = 0;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204); 
}

void draw() {
  val = (val + 1) % 255;
  background(val);
  myServer.write(val);
}

	

import processing.net.*; 

Client myClient; 
int dataIn; 
 
void setup() { 
  size(200, 200); 
  // Connect to the local machine at port 5204.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 5204); 
} 
 
void draw() { 
  print(myClient.available());
  dataIn = myClient.read(); 
  background(dataIn); 
}

Hello,

I was able to integrate this with the server and client examples and it works:
https://discourse.processing.org/t/scan-for-servers/21814/4

state = portIsOpen("127.0.0.1", 12345, 100);  //returns a true or false that you can use

:)

1 Like

Another related post:
https://discourse.processing.org/t/sending-a-processing-array-from-one-computer-to-other-computers/20366/5

It may be of interest to those using the network library.

:)

I visited the Processing gitHub client page and found out how to easily see if the server is available.

client.output != null

Processing receives an outputStream from a socket, but the object can be null.

Thanks anyway

1 Like