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
java.net.ConnectException: Connection refused: connect
And so on...
This does not stop program; just start the server and it will resume normally.
Processing code:
import processing.net.*;
Client c;
String input;
int data[];
boolean flag = true;
int count = 1000000;
void setup()
{
size(500, 100);
c = new Client(this, "127.0.0.1", 80);
frameRate(30);
}
void draw()
{
background(255, 255, 0);
if (c.active() == false)
{
fill(0);
textSize(48);
textAlign (CENTER, CENTER);
text("Server is not active!", width/2, height/2);
c = new Client(this, "127.0.0.1", 80);
}
if (c.available() > 0)
{
input = c.readString();
println(input);
input = input.substring(0, input.indexOf("\n"));
data = int(split(input, ' '));
// printArray(data);
}
}
void mousePressed()
{
if (mouseButton == LEFT && flag == true && c.active())
{
count++;
writeData();
flag = false;
println(count%1000000);
println();
}
if (mouseButton == RIGHT)
flag = true;
}
void writeData()
{
c.write
(
"POST /p00/p00.php HTTP/1.1\r\n" +
"Host: 127.0.0.1\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>
<html>
<body>
<?php
// https://stackoverflow.com/questions/12383371/refresh-a-page-using-php
$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>";
?>
</body>
</html>
*/
//p00.php on server
/*
<?php
//var $data;
$data=$_POST["name"];
file_put_contents("./USR_DATA.txt",$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.