Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Send an array throught Network (local only)

$
0
0

Hi everyone, I would like to know how to send an array throught the network, I've already tried but it don't seems to work Here is what i've already done :

Server side:

import processing.net.*;


boolean canIdraw = true ,envoye = false;

Server s;
Client c;
String input, pseudo;
int data[];
int [] data_terrain;



void setup() {
  size(500,500);
  background(204);
  stroke(0);
  data_terrain = new int[300]; // i generate an array w/ 300 int inside

  for ( int i = 0 ; i < 300 ; i++) {
    data_terrain [i] = int(random(20,150));  // data between 20 and 150
     }



    s = new Server(this, 12345);    // I start my server on this port 12345, for the moment everything is launched on the same computer
}


void draw() {

  if(envoye){
    envoye= false ;
  for ( int i = 0 ; i < 300 ; i++) { // It sends the array, value by value
 s.write(data_terrain[i]);
 println(data_terrain[i]);        // it print every value, to check they are the same between the two codes

}
}
}

void mousePressed(){                                      // If the mouse is pressed, then it starts sending the array
  envoye = true ;
}

Client Side :

import processing.net.*;


Client c;
String input= ".";
int data[];
int [] data_terrain ;
int i =0;



void setup() {
  size(700,300);
  background(204);
  stroke(0);
  data_terrain = new int[300];




  c = new Client(this, "127.0.0.1", 12345);// local, w/ the same port of course
}

void draw() {

  //if it receive data from server
  if (c.available() > 0) {
    input = c.readString();
   background(255);            // just change the background to know if there is a connection
   data_terrain[i]=int(input); // save the value received and print it.
   println(data_terrain[i]);
   println(i);
   i++; // increased i, and wait until the next value
  }
}

If you have the answer, please explain, 'cause I can't find a way to do it anywhere in the web.

Have a good day

PS: Sorry, my english is not perfect at all


Viewing all articles
Browse latest Browse all 2896

Trending Articles