The project I am working on recently has a function that requires time-consuming tasks to run in the background. Although PHP is not very suitable for being a daemon process that resides in the background, since the main code of the project is based on PHP, if the daemon process runs in the background Changing to another language would be very inconvenient. Therefore, it is inevitable that communication between the Web side and the Daemon part will be involved, and Socket is a good way.
What is Socket
The original English meaning of socket is "hole" or "socket". As the process communication mechanism of BSD UNIX, the latter meaning is taken. Also commonly called a "socket", it is used to describe an IP address and port, and is a handle to a communication chain. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and is bound to a port. Different ports correspond to different services.
The above content comes from Baidu Encyclopedia
Simply put, sockets can help different services communicate on different ports.
Implementation in PHP
### 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()));
- // Set blocking mode
- socket_set_block($socket)
- or die( "socket_set_block() failed:" . socket_strerror(socket_last_error()));
- // Bind to 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) {
- // Receive the connection request and call a sub-connection Socket to process the 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 communication
- socket_close($msgsock);
- }
- socket_close($socket);
- ?>
Copy code
The server mainly performs the following steps:
Create a Socket listener and wait for connections
When a link arrives, open a sub-connection to handle IO
Receive transmission data from client
Write the results back to the client
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 "Send successfully! n";
- }
- $out = '';
- while($buf = socket_read($socket, 8192)) {
- $out .= $buf;
- }
- echo "The accepted content is: $out n";
- socket_close($socket);
- ?>
Copy code
The client mainly has the following steps:
Connect to the server Socket
Write data to the server
Receive data from the server
Source: Yan Su’s blog
|