Good day, dear inhabitants of HashCode. Trying to write a simple socket-echo server on php. yes that there, I will just copy a code from http://www.php.net/manual/ru/sockets.examples.php

<?php error_reporting(E_ALL); /* Позволяет скрипту ожидать соединения бесконечно. */ set_time_limit(0); /* Включает скрытое очищение вывода так что мы получаем данные * как только они появляются. */ ob_implicit_flush(); $address = 'localhost'; $port = 10000; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "Не удалось выполнить socket_create(): причина: " . socket_strerror(socket_last_error()) . "\n"; } if (socket_bind($sock, $address, $port) === false) { echo "Не удалось выполнить socket_bind(): причина: " . socket_strerror(socket_last_error($sock)) . "\n"; } if (socket_listen($sock, 5) === false) { echo "Не удалось выполнить socket_listen(): причина: " . socket_strerror(socket_last_error($sock)) . "\n"; } do { if (($msgsock = socket_accept($sock)) === false) { echo "Не удалось выполнить socket_accept(): причина: " . socket_strerror(socket_last_error($sock)) . "\n"; break; } /* Отправляем инструкции. */ $msg = "\nДобро пожаловать на тестовый сервер PHP. \n" . "Чтобы отключиться, наберите 'выход'. Чтобы выключить сервер, наберите 'выключение'.\n"; socket_write($msgsock, $msg, strlen($msg)); do { if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { echo "Не удалось выполнить socket_read(): причина: " . socket_strerror(socket_last_error($msgsock)) . "\n"; break 2; } if (!$buf = trim($buf)) { continue; } if ($buf == 'выход') { break; } if ($buf == 'выключение') { socket_close($msgsock); break 2; } $talkback = "PHP: Вы сказали '$buf'.\n"; socket_write($msgsock, $talkback, strlen($talkback)); echo "$buf\n"; } while (true); socket_close($msgsock); } while (true); socket_close($sock); ?> 

I run this script ( http: //localhost/server.php ) - it does not output anything, it just simply loads endlessly. Okay, I'm trying to go to the next browser window at http: // localhost: 10000 - nifiga. As advised in the article, I open the terminal and enter:

 $ telnet localhost 10000 

I see in response:

 Trying 127.0.0.1... Connected to localhost. Escape character is '^]' 

.

And ... everything. What's wrong?

  • Yes, everything seems the same. What did you expect to see? - thunder
  • one
    Try to enter the terminal in the terminal) it is waiting for you to enter - thunder
  • : ~ $ telnet localhost 10000 Trying 127.0.0.1 ... Connected to localhost. Escape character is '^]'. hello! HELLO !!! ... I've tried. He keeps silence - Pavel Sotnikov
  • I tried avp-ubu1 3.2.0-49-generic in Linux # 75-Ubuntu SMP Tue Jun 18 17:39:32 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux Ubuntu 12.04.2 LTS \ n \ l and it works. For some reason, the problem with the Russian letter 'x' in the word "exit" in telnet (obviously PHP has nothing to do with it), but with ncat localhost 10000 everything is OK. But after if (false === ($ buf = socket_read ($ msgsoc .... ... probably instead of break 2 it’s better to just break) Well, you probably need to check errno (or whatever in PHP) if the client just broke the connection , then count as a "exit" command. - avp
  • Linux localhost 3.8.0-26-generic # 38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux Ubuntu 13.04 \ n \ l localhost for some reason is silent, ignore telnet ... - Pavel Sotnikov

1 answer 1

In general, as if this example is a worker, so I’ve brought the only thing from my code to the test that I can see something with the encoding. The answers are very strange: http://youtu.be/If_dguRmKVs

  • one
    Thank you for the video! = D Why do you think it does not start with me? (ubuntu 04/13) - Pavel Sotnikov
  • also 13.04 what is on the video. I do not know if the Apache and php can be configured in a special way - binliz