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

Application for pure.box:

Simple TCP echo server with PHP

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


When data are received (via TCP or UDP), the pure.box is able to respond by running a PHP script. The received data can be read via PHP and further processed (e.g. reply to the data source, transmission over the network or saving in the box-internal database). The PHP interpreter works exclusively "under the hood", and no traditional output in the form of a Web page is generated. This functionality allows you to realize simple server applications simply using Web techniques.

Objective

To test a network connection, an echo server that listens to TCP Port 8000 needs to be set up on the pure.box. Data sent to this port then need to be returned unchanged to the source.

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

Configuration of the pure.box

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

  • Open the Homepage of the pure.box 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 when data are sent to TCP port 8000 on the pure.box, they are returned to the sender. Returning the data requires receipt of a line break (LF / Return key) as a terminator. In general you should note that running of the PHP interpreter is automatically stopped after two minutes. The programmed action should therefore be capable of being processed in a shorter time.

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 = ’’;
do
	{
	$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.

fclose($in);
fclose($out);

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.

Summary

Simple server applications can be implemented with the pure.box with no prior knowledge beyond PHP. The server functionality is already part of the pure.box. All that is required is that the action you want to run when data are received is programmed.