topical media & game development

talk show tell print

graphic-processing-learning-19-example-19-3-example-19-3.pde / pde



  // 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());
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.