Wow! Thank you so much dimkir, it works perfectly! You're the best! I thought for sure I was screwed, I had no idea you could do clientName.write() in the server program!
Also thanks for contributing and mentioning Wireshark, PhiLho, I still have a lot to learn about networking, so any information like that is really helpful.
If anyone wants to see my implementation (which, warning, is just an example, and could probably be improved upon), here it is:
Server:
And it will work for any client. My particular client code is basically just a copy of the example (which helps demonstrate that each client is indeed getting its own data):
Also thanks for contributing and mentioning Wireshark, PhiLho, I still have a lot to learn about networking, so any information like that is really helpful.
If anyone wants to see my implementation (which, warning, is just an example, and could probably be improved upon), here it is:
Server:
- //Example code for a server with multiple clients communicating to only one at a time.
- import processing.net.*;
- Server mainServer;
- int val[] = new int[0];
- Client c[] = new Client[0];//the array of clients
- void setup() {
- size(200, 200);
- mainServer = new Server(this, 5204);
- noStroke();
- val = expand(val, val.length+1);
- }
- void draw() {
- background(0);
- textAlign(CENTER);
- text(c.length, width/2., height/2.);//Displays how many clients have connected to the server
- for (int i = 0; i < c.length; i++)
- {
- val[i] = (val[i]+i+1)%255;//changes the value based on which client number it has (the higher client number, the fast it changes).
- c[i].write(byte(val[i]));//writes to the right client (using the byte type is not necessary)
- text(val[i], width/2., height/2.+15*(i+1));
- }
- }
- void serverEvent(Server srvr, Client clnt)//Called when a client connects.
- {
- c = (Client[]) expand(c, c.length+1);//expand the array of clients
- c[c.length-1] = clnt;//sets the last client to be the newly connected client
- doStuff();
- }
- void doStuff()//this is extra stuff that can be done,
- {//in this case expanding a value array to have a value for each client.
- val = expand(val, val.length+1);
- }
And it will work for any client. My particular client code is basically just a copy of the example (which helps demonstrate that each client is indeed getting its own data):
- import processing.net.*;
- Client myClient;
- int dataIn;
- void setup() {
- size(200, 200);
- myClient = new Client(this, "127.0.0.1", 5204);
- }
- void draw() {
- textAlign(CENTER);
- background(0);
- background(dataIn);
- text("Client #clientNumber data: "+int(dataIn), width/2., height/2.);// you can replace clientNumber with whatever number you wish
- fill(255,0,0);
- for(int i = 0; true; i++)
- {
- if (myClient.available() > 0) {
- dataIn = myClient.read();
- }
- else
- break;
- }
- }