How do I send data to only one client (using the network library)? [SOLVED!]
in
Core Library Questions
•
1 month ago
When I use serverName.write() it sends out data to every client. I want to only send data to one client even when connected to multiple clients. I've tried creating an array of servers so that the client would only connect to one of them, but that didn't work; when it called serverName[serverNumber].write() it still wrote to all clients connected to the entire server program. If its relevant, the two client programs and one server program were all running on my computer. This was my attempt:
The server:
The client:
Also, I think I posted something similar to this over a year ago but IIIRC nobody else ever replied, so sorry if I'm spamming.
The server:
- import processing.net.*;
- Server littleServer[] = new Server[0];
- Server mainServer;
- int val[] = new int[0];
- void setup() {
- size(200, 200);
- mainServer = new Server(this, 5204);
- noStroke();
- val = expand(val, val.length+1);
- }
- void draw() {
- background(0);
- textAlign(CENTER);
- text(littleServer.length, width/2., height/2.);
- for (int i = 0; i < littleServer.length; i++)
- {
- val[i] = (val[i]+i+1)%255;
- littleServer[i].write(byte(val[i]));
- text(val[i], width/2., height/2.+15*(i+1));
- //println(i+"'s value is: "+val[i]);
- }
- }
- void serverEvent(Server srvr, Client clnt) {
- littleServer = (Server[]) expand(littleServer, littleServer.length+1);
- littleServer[littleServer.length-1] = srvr;
- doStuff();
- }
- void doStuff()
- {
- val = expand(val, val.length+1);
- if (val.length>1)
- val[val.length-1] = val[val.length-2];
- }
The client:
- 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);
- for(int i = 0; true; i++)
- {
- if (myClient.available() > 0) {
- dataIn = myClient.read();
- //text("Client 2 data: "+int(dataIn), width/2., height/2.);
- //background(dataIn);
- if(i >= 1)
- {
- text("MULTIPLE INPUT", width/2., height/2.);
- }
- }
- else
- break;
- }
- }
Also, I think I posted something similar to this over a year ago but IIIRC nobody else ever replied, so sorry if I'm spamming.
1