PHP Listening socket

Source: Internet
Author: User
Tags php server
PHP Listening socket




  1. Set_time_limit (10);
  2. $commonProtocol = Getprotobyname ("TCP");
  3. $socket = Socket_create (Af_inet, Sock_stream, $commonProtocol);
  4. if ($socket) {
  5. $result = Socket_bind ($socket, ' localhost ', 1337);
  6. if ($result) {
  7. $result = Socket_listen ($socket, 5);
  8. if ($result) {
  9. echo "Monitor success";
  10. }
  11. }
  12. }else{
  13. echo "Monitor failed";
  14. }
  15. do {
  16. if ($msgsock = socket_accept ($socket))) {/* Send a message to the user who is connected */
  17. $msg = "==========================================\r\n".
  18. "Welcome to the PHP Test Server. \r\n\r\n ".
  19. "To quit, type ' quit '." \ r \ n ".
  20. "To shut down the server type ' shutdown '. \ r \ n".
  21. "To get help message type ' help '. \ r \ n".
  22. "==========================================\r\n".
  23. "Php>";
  24. }
  25. Socket_write ($msgsock, $msg, strlen ($msg));
  26. do {
  27. $buf = Socket_read ($msgsock, 2048, php_binary_read);
  28. if (false = = = $buf) {
  29. echo "Socket_read () Failed:reason:". Socket_strerror ($result). "\ n";
  30. Break 2;
  31. }
  32. if (! $buf = Trim ($buf)) {
  33. Continue
  34. }/* Client input QUIT command when closing client connection */
  35. if ($buf = = ' Q ') {
  36. Break
  37. }/* Client input Shutdown command when both server and client are off */
  38. if ($buf = = ' shutdown ') {
  39. Socket_close ($msgsock);
  40. Break 2;
  41. }///Client Input help command when output assistance information */
  42. if ($buf = = ' h ') {
  43. $msg = "PHP Server help Message \r\n\r\n".
  44. "To quit, type ' quit '." \ r \ n ".
  45. "To shut down the server type ' shutdown '. \ r \ n".
  46. "To get help message type ' help '. \ r \ n".
  47. "Php>";
  48. Socket_write ($msgsock, $msg, strlen ($msg));
  49. Continue
  50. }/* Prompt message when the client input command does not exist */
  51. $talkback = "php:unknow command ' $buf ' .\r\nphp>";
  52. Socket_write ($msgsock, $talkback, strlen ($talkback));
  53. echo "$BUF \ n";
  54. } while (true);
  55. Socket_close ($msgsock);
  56. }while (TRUE);
  57. /* Close the socket connection */
  58. Socket_close ($socket);
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.