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
- Set_time_limit (0 );
- // Set host and port
- $ Host = "127.0.0.1 ";
- $ Port = 12387;
- // Create a tcp stream
- $ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)
- Or die ("socket_create () failed:". socket_strerror (socket_last_error ()));
- // Sets the blocking mode.
- Socket_set_block ($ socket)
- Or die ("socket_set_block () failed:". socket_strerror (socket_last_error ()));
- // Bind to the port
- Socket_bind ($ socket, $ host, $ port)
- Or die ("socket_bind () failed:". socket_strerror (socket_last_error ()));
- // Start Listening
- Socket_listen ($ socket, 4)
- Or die ("socket_listen () failed:". socket_strerror (socket_last_error ()));
- Echo "Binding the socket on $ host: $ port... \ n ";
- While (true ){
- // Receives connection requests and calls a sub-connection Socket to process information between the client and the server
- If ($ msgsock = socket_accept ($ socket) <0 ){
- Echo "socket_accept () failed:". socket_strerror (socket_last_error ());
- } Else {
- // Read data
- $ Out = '';
- While ($ buf = socket_read ($ msgsock, 8192 )){
- $ Out. = $ buf;
- }
- // Write data
- $ In = "data is $ out ";
- Socket_write ($ msgsock, $ in, strlen ($ in ));
- }
- // End the communication
- Socket_close ($ msgsock );
- }
- Socket_close ($ socket );
- ?>
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.
- Set_time_limit (0 );
- $ Host = "127.0.0.1 ";
- $ Port = 12387;
- // Create a tcp stream
- $ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)
- Or die ("socket_create () failed:". socket_strerror (socket_last_error ()));
- Echo "try to connect to $ host: $ port... \ n ";
- $ Result = socket_connect ($ socket, $ host, $ port)
- Or die ("socket_connect () failed:". socket_strerror (socket_last_error ()));
- $ In = "hello \ n ";
- If (! Socket_write ($ socket, $ in, strlen ($ in ))){
- Echo "socket_write () failed:". socket_strerror ($ socket );
- } Else {
- Echo "sent successfully! \ N ";
- }
- $ Out = '';
- While ($ buf = socket_read ($ socket, 8192 )){
- $ Out. = $ buf;
- }
- Echo "accept content: $ out \ n ";
- Socket_close ($ socket );
- ?>
The client has the following steps: Connect to the server Socket to write data to the server to receive data from the serverSource: Yan Su's blog |