Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 19-3: Server broadcasting a number (0-255)

// Import the net libraries
import processing.net.*;

// Declare a server
Server server;
PFont f;
int data = 0;

void setup() {
  size(200,200);
  // Create the Server on port 5204
  server = new Server(this, 5204);
  f = createFont("Arial",20,true);
}

void draw() {
  background(255);
  
  // Display data
  textFont(f);
  textAlign(CENTER);
  fill(0);
  text(data,width/2,height/2);
  
  // Broadcast data (the number is continuously sent to all clients because write() is called every cycle through draw())
  server.write(data);
  
  // Arbitrarily changing the value of data randomly
  data = (data + int(random( -2,4))) % 256;
}

// The serverEvent function is called whenever a new client connects.
void serverEvent(Server server, Client client) {
  println(" A new client has connected: "+ client.ip());
}
  • Anonymous

    Hi!
    Is it possible to use it for big distances, like china or at least another country? Because it works fine to another computer using the same IP adress. But what if the other person uses another IP adress than i do… how to fix it up correctly? I dont even know if its because of the IP adress or if there is someting more to set up.. ?
    cheers

  • http://www.facebook.com/shiffman Daniel Shiffman

    Yup, as long as you know the IP addresses of the machine and that the port isn’t blocked you can!