Working example below that will receive data in the server sketch.
Server and client (start first)
// Server and client
import processing.net.*;
Server server0;
Client client0;
void setup()
{
size(400, 200);
server0 = new Server(this, 5204); // Listen on port 5204
println("Server started on port 5204");
}
void draw()
{
}
void serverEvent(Server server, Client client)
{
println("New client connected: " + client.ip());
server.write("Start sending...");
}
void clientEvent(Client myClient)
{
print("Client0 received: ");
String dataIn = myClient.readString();
println(dataIn);
}
Client (start second)
// Client
import processing.net.*;
Client client1;
boolean sendData = false;
int count = 0;
void setup()
{
size(200, 200);
client1 = new Client(this, "127.0.0.1", 5204);
}
void draw()
{
// Sends data every 60 frames
if (sendData)
{
if (frameCount%60 == 0) // Every 1 sec at if frameRate = 60
{
println("Client1 sending: " + count);
client1.write(str(count)); // Sends data to server
count++;
}
}
}
// Once the client connects to server it starts sending data
void clientEvent(Client myClient)
{
print("Server Says: ");
String dataIn = myClient.readString();
println(dataIn);
if(dataIn.equals("Start sending..."))
sendData = true;
}
I used the console to show messages and data sent and received.
You will have to make each sketch active (click with mouse) to see updates (sending and receiving) in console.
You could also receive the data in draw():
// Server and client
import processing.net.*;
Server server0;
Client client0;
void setup()
{
server0 = new Server(this, 5204); // Listen on port 5204
println("Server started on port 5204");
}
void draw()
{
// Get the next available client
client0 = server0.available();
if (client0 != null)
{
println("Client connected: " + client0.ip());
while (client0.available() > 0)
{
// Read the data sent by the client1
String msg = client0.readString();
{
println("Received: " + msg);
}
}
}
}
void serverEvent(Server server, Client client)
{
println("New client connected: " + client.ip());
server.write("Start sending...");
}
//void clientEvent(Client myClient)
// {
// print("Client0 received: ");
// String dataIn = myClient.readString();
// println(dataIn);
// }
Do you have a complete working example (server and client code) including that code snippet?
Please post this.
I’m interested in seeing this because I couldn’t get it to work.
I did manage to get your code snippet to work integrated with my working example and some adjustments.
I had to add a delay() so that the client data that was sent was buffered and would be available after the connection to receive it.
I also removed client.port() as it is not available in the network library.
It only received data while it was available… 19 elements buffered.
With delay(20000) and sending data every 1 sec there were 20 elements in the buffer to receive and then it exited from the serverEvent() and stop receiving data that continued to be sent by the client.
Cool! I asked an AI to translate (it also commented) to English and tested and it works!
I am always interested in the process for finding solutions and\or the source of code.
What was the development process for finding this solution, source of code or inspiration for this?
For me, development typically involves a combination of reading, research, hands-on experience, working with large language models like ChatGPT and OpenGemini, and continuous testing.
Lately, I’ve been using LLMs more frequently and had an interesting experience engaging in a step-by-step dialogue where I had to correct the model multiple times and often receiving apologies in return. Many of the suggested solutions didn’t work initially, so it became a very iterative process. At one point, I even had to ‘educate’ the model by providing Processing source code and links to relevant references. It thanked me but then forgot later! In the end it did provide some insight and added value to the discussion. It is useful as long as you have experience and scrutinize the solutions offered.
There are different libraries used and some advanced topics.
Some research will provide clarity.
I found ChatGPT and Gemini useful for:
Commenting\explaining code.
It also made suggestions for consideration.
Converting code from Processing only library to Java library and vice versa.
Keep in mind that all ChatGPT code and responses should be scrutinized!
It may be incorrect and very apologetic for it. It is doing unsupervised training and self learning along the way and sometimes has incorrect answers.
Thanks @GWAK and @jafal for code provided.
The code provided looks like a solid implementation using familiar and widely adopted components and code which was adapted for the Processing environment.
It took a lot research on my part to sift through all the code and thoroughly understand the implementation.
It is important to provide context with code that is shared: Guidelines—Answering Questions
Comments and references are important to provide context; I will have to discipline myself to do that and posted this to help others with the code provided. I sometimes look at my older code and have no idea what I was doing from lack of comments or references!
I certainly enjoyed the exploration of this but it may be a challenge for others.