Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

How do I send data to only one client (using the network library)? [SOLVED!]

$
0
0
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:
  1. import processing.net.*;
  2. Server littleServer[] = new Server[0];
  3. Server mainServer;
  4. int val[] = new int[0];

  5. void setup() {
  6.   size(200, 200);
  7.   mainServer = new Server(this, 5204);
  8.   noStroke();
  9.   val = expand(val, val.length+1);
  10. }
  11. void draw() {
  12.   background(0);
  13.   textAlign(CENTER);
  14.   text(littleServer.length, width/2., height/2.);
  15.   for (int i = 0; i < littleServer.length; i++)
  16.   {
  17.     val[i] = (val[i]+i+1)%255;
  18.     littleServer[i].write(byte(val[i]));
  19.     text(val[i], width/2., height/2.+15*(i+1));
  20.     //println(i+"'s value is: "+val[i]);
  21.   }
  22. }
  23. void serverEvent(Server srvr, Client clnt) {
  24.   littleServer = (Server[]) expand(littleServer, littleServer.length+1);
  25.   littleServer[littleServer.length-1] = srvr;
  26.   doStuff();
  27. }
  28. void doStuff()
  29. {
  30.   val = expand(val, val.length+1);
  31.   if (val.length>1)
  32.   val[val.length-1] = val[val.length-2];
  33. }
The client was essentially the same as the processing example for Client, but displays the text "MULTIPLE INPUT" to the program's screen if it gets 2 bytes or more in its buffer (which I don't think is a perfect test, but with other tests it seems to confirm that it is indeed getting two different values from the two different litttleServers).
The client:

  1. import processing.net.*;
  2. Client myClient;
  3. int dataIn;
  4. void setup() {
  5.   size(200, 200);
  6.   myClient = new Client(this, "127.0.0.1", 5204);
  7. }
  8. void draw() {
  9.   textAlign(CENTER);
  10.   background(0);
  11.   for(int i = 0; true; i++)
  12.   {
  13.     if (myClient.available() > 0) {
  14.       dataIn = myClient.read();
  15.       //text("Client 2 data: "+int(dataIn), width/2., height/2.);
  16.       //background(dataIn);
  17.       if(i >= 1)
  18.       {
  19.         text("MULTIPLE INPUT", width/2., height/2.);
  20.       }
  21.     }
  22.     else
  23.     break;
  24.   }
  25. }

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.

Viewing all articles
Browse latest Browse all 1768

Trending Articles