Want some programming help? [close]

Simple PHP TCP Server

With PHP, creating a TCP server takes only a few lines of code. Check out this code that creates a server which responds with the current time.

<?php
$socket = stream_socket_server("tcp://192.168.1.5:8000", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)\n";
  die('Could not create socket');
}

while (true) {
  while ($conn = stream_socket_accept($socket, -1, $peername)) {
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    echo "Connection received from: $peername\n";
    fclose($conn);
  }
}

fclose($socket);

Advertisement