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 

  • 相关阅读:
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    HDU 5627 Clarke and MST &意义下最大生成树 贪心
    HDU 5626 Clarke and points 平面两点曼哈顿最远距离
    《花开物语》观后感
    Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
    Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
    Educational Codeforces Round 7 E. Ants in Leaves 贪心
    Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11517731.html
Copyright © 2011-2022 走看看