# Networking: Processing Client to PHP Server

**URL:** https://discourse.processing.org/t/networking-processing-client-to-php-server/12466
**Category:** Gallery
**Created:** [July 3, 2019, 12:08am UTC](https://discourse.processing.org/t/networking-processing-client-to-php-server/12466 "2019-07-03T00:08:50Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 3, 2019, 12:08am UTC](https://discourse.processing.org/t/networking-processing-client-to-php-server/12466/1 "2019-07-03T00:08:50Z")

</div>

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

> **[WampServer](https://sourceforge.net/projects/wampserver/)**
>
> Download WampServer for free. A Windows Web development environment for Apache, MySQL, PHP databases. WampServer is a Web development platform on Windows that allows you to create dynamic Web applications with Apache2, PHP, MySQL and MariaDB....

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:

```auto
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.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c6fb05e9a94abdf6f9ccc57c96c17f59bb85ba9f.png)

Processing code:

```auto
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.

🙂
