PHPSocket programming implementation

Source: Internet
Author: User
PHPSocket programming implementation

Recently, a project has a function that requires time-consuming tasks to run in the background. Although PHP is not very suitable for resident backend Daemon, the main code of the project is implemented based on PHP, if the daemon running in the background changes to another language, it is very inconvenient. Therefore, it will inevitably involve communication between the Web end and the Daemon part. Socket is a good method.

What is Socket?

The original meaning of socket is "hole" or "socket ". As the process communication mechanism of bsd unix, take the latter meaning. It is also called "socket". it is used to describe the IP address and port and is a communication chain handle. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and binds it to a port. different ports correspond to different services.

The above content is from Baidu Baike

Simply put, socket can help different services communicate on different ports.

PHP implementation

### Server

  1. Set_time_limit (0 );
  2. // Set host and port
  3. $ Host = "127.0.0.1 ";
  4. $ Port = 12387;
  5. // Create a tcp stream
  6. $ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)
  7. Or die ("socket_create () failed:". socket_strerror (socket_last_error ()));
  8. // Sets the blocking mode.
  9. Socket_set_block ($ socket)
  10. Or die ("socket_set_block () failed:". socket_strerror (socket_last_error ()));
  11. // Bind to the port
  12. Socket_bind ($ socket, $ host, $ port)
  13. Or die ("socket_bind () failed:". socket_strerror (socket_last_error ()));
  14. // Start Listening
  15. Socket_listen ($ socket, 4)
  16. Or die ("socket_listen () failed:". socket_strerror (socket_last_error ()));
  17. Echo "Binding the socket on $ host: $ port... \ n ";
  18. While (true ){
  19. // Receives connection requests and calls a sub-connection Socket to process information between the client and the server
  20. If ($ msgsock = socket_accept ($ socket) <0 ){
  21. Echo "socket_accept () failed:". socket_strerror (socket_last_error ());
  22. } Else {
  23. // Read data
  24. $ Out = '';
  25. While ($ buf = socket_read ($ msgsock, 8192 )){
  26. $ Out. = $ buf;
  27. }
  28. // Write data
  29. $ In = "data is $ out ";
  30. Socket_write ($ msgsock, $ in, strlen ($ in ));
  31. }
  32. // End the communication
  33. Socket_close ($ msgsock );
  34. }
  35. Socket_close ($ socket );
  36. ?>


The server performs the following steps:

Create a Socket listener and wait for the connection to start a sub-connection to process IO and receive data transmitted from the client to write the result back to the client.

  1. Set_time_limit (0 );
  2. $ Host = "127.0.0.1 ";
  3. $ Port = 12387;
  4. // Create a tcp stream
  5. $ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)
  6. Or die ("socket_create () failed:". socket_strerror (socket_last_error ()));
  7. Echo "try to connect to $ host: $ port... \ n ";
  8. $ Result = socket_connect ($ socket, $ host, $ port)
  9. Or die ("socket_connect () failed:". socket_strerror (socket_last_error ()));
  10. $ In = "hello \ n ";
  11. If (! Socket_write ($ socket, $ in, strlen ($ in ))){
  12. Echo "socket_write () failed:". socket_strerror ($ socket );
  13. } Else {
  14. Echo "sent successfully! \ N ";
  15. }
  16. $ Out = '';
  17. While ($ buf = socket_read ($ socket, 8192 )){
  18. $ Out. = $ buf;
  19. }
  20. Echo "accept content: $ out \ n ";
  21. Socket_close ($ socket );
  22. ?>


The client has the following steps:

Connect to the server Socket to write data to the server to receive data from the server

Source: Yan Su's blog

PHP, Socket

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.