Home > Backend Development > PHP Tutorial > PHP listening Socket

PHP listening Socket

WBOY
Release: 2016-07-25 08:47:27
Original
906 people have browsed it
PHP listening Socket
                 
                                                                                                                                                                                                                                                                                                                  


set_time_limit(10);
$commonProtocol = getprotobyname("tcp");
    $socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
  1. if ($socket) {
  2. $result = socket_bind($socket, 'localhost', 1337);
  3. if ($result) {
  4. $result = socket_listen($socket, 5);
  5. if ($result) {
  6. echo "Listening successful";
  7. }
  8. }
  9. }else{
  10. echo "Listening failed";
  11. }
  12. do {
  13. if (($msgsock = socket_accept($socket))) { /* Send prompt information to the connected user*/
  14. $msg = "======== ==================================rn" .
  15. "Welcome to the PHP Test Server. rnrn" .
  16. "To quit, type 'quit'.rn" .
  17. "To shut down the server type 'shutdown'.rn" .
  18. "To get help message type 'help'.rn" .
  19. "======= =====================================rn" .
  20. "php>";
  21. }
  22. socket_write( $msgsock, $msg, strlen($msg));
  23. do {
  24. $buf = socket_read($msgsock, 2048, PHP_BINARY_READ);
  25. if (false === $buf) {
  26. echo "socket_read() failed : reason: " . socket_strerror($result) . "n";
  27. break 2;
  28. }
  29. if (!$buf = trim($buf)) {
  30. continue;
  31. } /* Close the client when the client enters the quit command End connection*/
  32. if ($buf == 'q') {
  33. break;
  34. } /* When the client enters the shutdown command, both the server and the client are shut down*/
  35. if ($buf == 'shutdown') {
  36. socket_close($msgsock);
  37. break 2;
  38. } /* Output help information when the client enters the help command*/
  39. if ($buf == 'h') {
  40. $msg = "PHP Server Help Message rnrn".
  41. " To quit, type 'quit'. rn" .
  42. " To shut down the server type 'shutdown'.rn" .
  43. " To get help message type 'help'.rn" .
  44. "php> ";
  45. socket_write ($msgsock, $msg, strlen($msg));
  46. continue;
  47. } /* Prompt message when the client input command does not exist*/
  48. $talkback = "PHP: unknown command '$buf'.rnphp> ";
  49. socket_write($msgsock, $talkback, strlen($talkback));
  50. echo "$bufn";
  51. } while (true);
  52. socket_close($msgsock);
  53. }while (true);
  54. /* Close Socket connection */
  55. socket_close($socket);
  56. Copy code
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template