Wiesemann & Theis GmbH

Networking, sensors and interface technology for industry, office and IT

Application for Motherbox:

Simple TCP echo server with PHP

Execution of a PHP script when data are received on a TCP port

When data are received (via TCP or UDP), the Motherbox 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.


To test a network connection, an echo server that listens to TCP Port 8000 needs to be set up on the Motherbox. 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.

Configuring the Motherbox

Save the file echo.php using FTP or SMB sharing in the folder named programs on the Motherbox. Then the following configuration steps are necessary:

  • Open the Homepage of the Motherbox by entering the IP address in the address line of your browser.
    Motherbox homepage
  • Select Login in the menu tree and log in from the dialog box with administrator rights (user name: admin and your password).

    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 Motherbox, 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 = ’’;
							$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 Motherbox with no prior knowledge beyond PHP. The server functionality is already part of the Motherbox. All that is required is that the action you want to run when data are received is programmed.