fork download
  1. <?php
  2.  
  3.  
  4.  
  5. $address = '127.0.0.1';
  6. $port = 1337;
  7.  
  8. if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
  9. echo "Не удалось создать сокет, причина: " . socket_strerror(socket_last_error()) . "\n";
  10. }
  11.  
  12. if (socket_bind($sock, $address, $port) === false) {
  13. echo "Не удалось привязать сокет к адрресу, причина: " . socket_strerror(socket_last_error()) . "\n";
  14. }
  15.  
  16. if (socket_listen($sock, 5) === false) {
  17. echo "Не удалось выполнить socket_listen(): причина: " . socket_strerror(socket_last_error($sock)) . "\n";
  18. }
  19.  
  20. do {
  21. if (($msgsock = socket_accept($sock)) === false) {
  22. echo "Не удалось выполнить socket_accept(): причина: " . socket_strerror(socket_last_error($sock)) . "\n";
  23. break;
  24. }
  25.  
  26. socket_write($msgsock, "Welcome", strlen("Welcome"));
  27.  
  28. do {
  29. if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
  30. echo "не удалось прочитать сокет, причина: " . socket_strerror(socket_last_error($msgsock));
  31. break 2;
  32. }
  33.  
  34. if (!$buf = trim($buf)) {
  35. continue;
  36. }
  37.  
  38. if ($buf == 'exit') {
  39. break;
  40. }
  41.  
  42. socket_write($msgsock, $buf . "\n", strlen($buf));
  43. echo $buf . "\n";
  44. } while (true);
  45. socket_close($msgsock);
  46. } while (true);
  47.  
  48. socket_close($sock);
Time limit exceeded #stdin #stdout 5s 61648KB
stdin
Standard input is empty
stdout
Standard output is empty