PHP uses spool to write a simple echo server example


This article describes a simple echo server written by PHP using spool. The details are as follows:

server.php The code is as follows:

class EchoServer {
  protected $serv = null;
  public function __construct() {
    $this->serv = new swoole_server('', 8888);
    //Configuration parameters
      'worker_num' => 4,
      'daemonize' => 0,
    //Register callback function
    $this->serv->on('start', array($this, 'start'));
    $this->serv->on('connect', array($this, 'connect'));
    $this->serv->on('receive', array($this, 'receive'));
    $this->serv->on('close', array($this, 'close'));
    //Start the service
  public function start($serv) {
    echo "start \n";
  //When there is a client connection
  public function connect($serv, $fd) {
    echo "connect \n";
    $serv->send($fd, "hello \n");
  public function close($serv, $fd) {
    echo "close \n";
  public function receive($serv, $fd, $from_id, $data) {
    echo "get message {$fd} : {$data} \n";
    //Send information to client
    $serv->send($fd, $data . "\n");
$serv = new EchoServer();

client.php The code is as follows:

class EchoClient {
  protected $client = null;
  public function __construct() {
    //Note that it needs to be set to asynchronous, otherwise the event callback function cannot be set below
    $this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    $this->client->on('connect', array($this, 'connect'));
    $this->client->on('receive', array($this, 'receive'));
    $this->client->on('close', array($this, 'close'));
    $this->client->on('error', array($this, 'error'));
    //Connecting to the server
    $this->client->connect('', 8888);
  public function connect($client) {
    echo "connect \n";
  public function receive($client, $data) {
    echo "server send: {$data}";
    //Write data to standard output
    Fwrite (stdout, "please input message");
    //Get standard input data
    $msg = trim(fgets(STDIN));
    //Send data to server
  public function close($client) {
    echo "close \n";
  public function error($client) {
    echo "error \n";
$cli = new EchoClient();

Then run the two scripts separately

> /data/php56/bin/php server.php
> /data/php56/bin/php client.php

The results are as follows

For more information about PHP, readers who are interested in it can see the following topics: summary of PHP network programming skills, summary of PHP socket usage, introductory course of PHP object-oriented programming, tutorial of PHP data structure and algorithm, and summary of PHP programming algorithm

I hope this article is helpful for PHP programming.

Recommended Today

External part – Visual Studio code and vue.js

Fan waipian Seaconch is so tired that he can’t help writing this article I can’t help but recommend new editors. I can’t help but use them everywherevs codeThis editor To whom? I don’t know, when the same person is like meFrom vsThen maybe you will feel the same as me~ Start learning in Mayvue.jsUp to […]