zoukankan      html  css  js  c++  java
  • swore tcp服务学习

    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 SwooleServer(self::IP, self::PORT);
    
    		$this->serv->on("Connect",[$this,"onConnect"]);
    		$this->serv->on("Receive",[$this,"onReceive"]);
    		$this->serv->on("Close",[$this,"Onclose"]);
    	}
    
    	/**
    	 * 客户端连接触发
    	 * @param $serv 服务器信息
    	 * @param $fd 客户端标识
    	 */
    	public function onConnect($serv,$fd)
    	{
    		echo "客户端连接:".$fd.PHP_EOL;
    	}
    
    	/**
    	 * 收到客户端信息时候触发
    	 * @param $serv 服务器信息
    	 * @param $fd 客户端标识
    	 * @param $reactor_id 线程ID
    	 * @param $data 接受到的数据
    	 */
    	public function onReceive($serv,$fd,$reactor_id,$data)
    	{
    		echo "服务器接受到客户端-".$fd."-数据".$data." 线程ID-".$reactor_id.PHP_EOL;
    	}
    
    	/**
    	 * @param $serv 服务器信息
    	 * @param $fd 客户端标识
    	 */
    	public function onClose($serv,$fd)
    	{
    		echo "客户端-".$fd."-关闭连接".PHP_EOL;
    	}
    
    	/**
    	 * @param $config  配置
    	 */
    	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();
    

     进入 cli模式  执行

    php TcpServer.php 开始监听本机的9501端口

    ps -aft|grep TcpServer.php

    netstat -tlunp|grep 9501 

    可查看是否成功

    连接tcp  通过telnet  

    mac  brew install telnet

    linux(centos) yum -y install telnet 

    退出telnet 

    ctrl + ]

    然后 输入 ?号

    然后输入quit 

  • 相关阅读:
    NOI2021游记-记最后一段日子
    7.20 ZROI-Day7模拟赛
    7.19 ZROI-Day6模拟赛
    7.18 ZROI-Day5模拟赛
    7.17 ZROI-Day4模拟赛
    7.16 ZROI-DAY3 模拟赛
    7.15 ZROI-DAY2 模拟赛
    7.13 ZROI-DAY1赛后总结
    7.9模拟赛赛后总结
    7.7模拟赛赛后总结
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11517731.html
Copyright © 2011-2022 走看看