W&T connects
Interfaces for TCP/IP, Ethernet, RS-232, RS-485, USB, 20mA, glass and plastic fiber optic cable, http, SNMP, OPC, Modbus TCP, I/O digital, I/O analog, ISA, PCI


TCP Echo Server with PHP

Running a PHP script when data are received on a TCP port

The pure.box 2 is able to respond when data are received (transmission via TCP or UDP) by opening a PHP script. The received data can be read via PHP and then further processed (e.g. reply to data source, forwarding over the network or saving in the box internal database. The PHP interpreter works here only "under the hood," i.e. no classical output in the form of a web page is generated. This functionality lets you implement simple server applications, using only web technology.


To test a network connection you want to set up an echo server on the pure.box 2 which listens to TCP port 8000. Data sent to this port will then be returned to the source unchanged.

The PHP file for implementing this example, echo.php, can be downloaded here.

Configuration of the pure.box 2

Save the file echo.php using FTP or SMB sharing in the folder named programs on the pure.box 2. Then carry out the following configuration steps:

  • Open the Homepage of the pure.box 2 by entering the IP address in the address line of your browser.

    Motherbox homepage
  • Select Login in the menu tree and log on using your password in the opened dialog box.

    Motherbox login dialog box
  • Navigate to the page Home >> Configuration >> User files. In the file overview click on the folder named programs. The file echo.php is displayed.

    Motherbox file browser
  • Clicking on the icon Edit file properties (left next to echo.php) opens the Edit dialog.

    Edit file properties
  • Check the option Event controlled. Select TCP and set Port to 8000. Save your changes by clicking on Save... you’re done!

    Event control

Now if data are sent to TCP port 8000 on the pure.box 2, they are returned to the sender. The prerequisite for returning the data is reception of a line break (LF / Return key) as end character. In general it must be noted that running of the PHP interpreter is automatically stopped after two minutes. The programmed action should therefore be executable in less time than this.

Source text

In the first two lines the standard input for reading and the standard output for writing data is opened.

							$in = fopen(’php://stdin’, ’r’);
							$out = fopen(’php://stdout’, ’w’);

The incoming data are read character by character in a loop and the string variable $input is appended. The loop is exited when a line break (Line Feed, ASCII table: 10) is detected.

							$input = ’’;
								$input .= fread($in, 1);
							while(substr($input, -1, 1) != chr(10));

After receipt, the read string is sent back out to the initiating data source through the standard output.

fwrite($out, $input);

Then both the standard input and standard output are closed.


This PHP script has now been fully run and is exited. The next time data are received the script is restarted and runs again as described above.


Simple server applications can be implemented with the pure.box 2 just by knowing PHP. The server functionality is already built into the box. All that is left is to program the action you wish to occur when the data are received.