Home  >  Article  >  Backend Development  >  PHP Socket programming implementation

PHP Socket programming implementation

WBOY
WBOYOriginal
2016-07-25 08:45:17860browse

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

  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. // Set blocking mode
  9. socket_set_block($socket)
  10. or die( "socket_set_block() failed:" . socket_strerror(socket_last_error()));
  11. // Bind to 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. // Receive the connection request and call a sub-connection Socket to process the 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 communication
  33. socket_close($msgsock);
  34. }
  35. socket_close($socket);
  36. ?>
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
  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 "Send successfully! n";
  15. }
  16. $out = '';
  17. while($buf = socket_read($socket, 8192)) {
  18. $out .= $buf;
  19. }
  20. echo "The accepted content is: $out n";
  21. socket_close($socket);
  22. ?>
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

PHP, Socket


Statement:
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