<?php

$host = "127.0.0.1";
$port = "8888";

set_time_limit(0);

print "uruchamianie serwera...\n";

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_bind($sock, $host, $port);

socket_listen($sock, 4);

$childSocket = socket_accept($sock);

do
{
   // Czekaj na nowe komunikaty

   $incomingData = socket_read($childSocket, 2048, PHP_NORMAL_READ);

   if (trim($incomingData) == "Lubisz mleko?")
   {
      $response = "Odpowiedź serwera > Tak, bardzo!\n";
      socket_write($childSocket, $response, strlen($response));
   }
   else if (trim($incomingData) == "koniec")
   {
      $response = "Do widzenia!\n";
      socket_write($childSocket, $response, strlen($response));
      socket_close($childSocket);
      break;
   }
   else
   {
      $response = strtoupper(trim($incomingData)) . "\n";
      $writeResp = socket_write($childSocket, $response, strlen($response));
      if ($writeResp === FALSE)
      {
         socket_close($childSocket);
         break;
      }
   }

}
while (true);

socket_close($sock);

?>
