swore tcp service learning

TcpServer.php

<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2019/9/13
 * Time: 20:33
 */

class TcpServer
{
	const IP = "0.0.0.0";
	const PORT = 9501;

	public $serv;


	//创建Server对象,监听 本机9501端口
	public function __construct()
	{

		$this->serv = new Swoole\Server(self::IP, self::PORT);

		$this->serv->on("Connect",[$this,"onConnect"]);
		$this->serv->on("Receive",[$this,"onReceive"]);
		$this->serv->on("Close",[$this,"Onclose"]);
	 * /
	 * @param $ fd client identifier
	 * @param $ serv server information
	 * client connections trigger
	/ **
	}

	public function onConnect ($ serv, $ 
	{ 
		echo "client connection:." $ fd.PHP_EOL; 
	} 

	/ ** 
	 * received client information when triggered 
	 * @param $ serv server information 
	 * @param $ fd client identifying 
	 * @param $ reactor_id thread ID 
	 * @param $ data received data 
	 * / 
	public function the onReceive (Serv $, $ FD, reactor_id $, $ data) 
	{ 
		echo "server receives a client -".. $ fd " - data. "$ data." thread ID - "$ reactor_id.PHP_EOL;. 
	} 

	/ ** 
	 * @param $ Serv server information 
	 * @param $ fd client identifier 
	 * / 
	public function the onClose (Serv $, $ FD) 
	{ 
		.. echo "client -" $ fd "- close the connection" .PHP_EOL; 
	} 

	/ ** 
	 * @param $ config configuration 
	 * / 
	public function SET (Array $ config) 
	{
		$this->serv->set($config);
	}

	public function start()
	{
		$this->serv->start();
	}
}

$tcp = new TcpServer();
$tcp->set(array(
	'reactor_num' => 2, //reactor thread num
	'worker_num' => 4,    //worker process num
	'backlog' => 128,   //listen backlog
	'max_request' => 50,
	'dispatch_mode' => 1));
$tcp->start();

 Enter cli-mode execution

php TcpServer.php starts listening to port 9501 of the machine

ps -aft|grep TcpServer.php

netstat -tlunp|grep 9501 

To view success

 

Tcp connection via telnet  

mac  brew install telnet

linux(centos) yum -y install telnet 

 

 

 

Exit telnet 

ctrl + ]

Then enter? number

Then enter quit 

 

Guess you like

Origin www.cnblogs.com/php-linux/p/11517731.html