Networking: Processing Client to PHP Server

A very simple example of a Processing network client communicating with a PHP server.
I used the WAMP server:

An “update” increments a counter (Processing) and is sent to a server( with c.write()) and stored in a file on server.
You can run the web page (reads “counter” from file) to watch the updates or just open the file to see current status.
Restarting the Processing sketch resets the counter on Processing and the saved counter on server.

“Updates” are sent with a mouse left button click.
A right mouse button click is required before next update; this was to control update rate so it was not every frame. Updates can be replace with a timer or as required.

“Client got end-of-stream.” message can be expected.

Server should be running otherwise a warning is displayed in draw window and console fills with:

Client SocketException: Connection reset Connection refused: connect
And so on...

This does not stop program; just start the server and it will resume normally.

Processing code:


Client c;
String input;
int data[];
boolean flag = true;
int count = 1000000;

void setup() 
  size(500, 100);
  c = new Client(this, "", 80);

void draw() 
  background(255, 255, 0);
  if ( == false)
    textAlign (CENTER, CENTER);
    text("Server is not active!", width/2, height/2);
    c = new Client(this, "", 80);
  if (c.available() > 0) 
    input = c.readString();
    input = input.substring(0, input.indexOf("\n"));
    data = int(split(input, ' '));
//    printArray(data);
  void mousePressed() 
    if (mouseButton == LEFT && flag == true &&
      flag = false;
    if (mouseButton == RIGHT) 
      flag = true;

void  writeData()
  "POST /p00/p00.php HTTP/1.1\r\n" +
  "Host:\r\n" +
  "Content-Type: application/x-www-form-urlencoded\r\n" +
  "Content-Length: 14\r\n\r\n" +
  "name=" + count + "\r\n"
// index.php on server:
<!DOCTYPE html>

$page = $_SERVER['PHP_SELF'];
$sec = "1";
header("Refresh: $sec; url=$page");

$data = file_get_contents("USR_DATA.txt")."\r";
print "<h3>Hello Folks!</h4>";
$a = 1000000;
$b = (int)$data;
$c = $b - $a;

print "<h3>Counter:</h4>";
print "<h3>".($c)."</h3>";


//p00.php on server
//var $data;

I am not providing support for this code or server setup.
I had too much fun doing this and want you to share that experience.