Here there is a simple example of tcp server written in php that accepts a connection from a client and for each message that the client sends to the server, it reply with the same message.

<?php

/* Allow the script to hang around waiting for connections. */

set_time_limit(0);

/* Turn on implicit output flushing so we see what we’re getting

* as it comes in. */

ob_implicit_flush();

$address = ‘100.220.110.5’;

$port = 10001;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {

echo “socket_create() failed: reason: ” . socket_strerror(socket_last_error()) . “\n”;

}

if (socket_bind($sock, $address, $port) === false) {

echo “socket_bind() failed: reason: ” . socket_strerror(socket_last_error($sock)) . “\n”;

}

if (socket_listen($sock, 5) === false) {

echo “socket_listen() failed: reason: ” . socket_strerror(socket_last_error($sock)) . “\n”;

}

do {

if (($msgsock = socket_accept($sock)) === false) {

echo “socket_accept() failed: reason: ” . socket_strerror(socket_last_error($sock)) . “\n”;

break;

}

do {

if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {

echo “socket_read() failed: reason: ” . socket_strerror(socket_last_error($msgsock)) . “\n”;

break 2;

}

$talkback = “PHP: You said ‘$buf’.\n”;

socket_write($msgsock, $talkback, strlen($talkback));

echo “$buf\n”;

} while (true);

socket_close($msgsock);

} while (true);

socket_close($sock);

?>

To see a tcp client (written in PHP): http://grassi86.altervista.org/client-tcp-with-php/

To see how to run this script on a linux shell:  http://grassi86.altervista.org/how-to-run-php-script-on-linux-shell/