zoukankan      html  css  js  c++  java
  • PHP swoole TCP服务端和客户端

    • 服务端
    <?php
    $server = new swoole_server('127.0.0.1',9501,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
    $server->set(array(
        'woker_num'=>4,
        'max_request'=>1000
    ));
    $server->on('connect',function($serv,$fd,$reactor_id)
    {
        echo "接收到来自$reactor_id"."的连接符$fd
    ";
    });
    $server->on('receive',function ($serv,$fd,$reactor_id,$data)
    {
        echo "收到{$reactor_id}----{$fd}"."发来的数据$data
    ";
        $serv->send($fd,"{$reactor_id}---{$fd}发来的数据{$data}");
    });
    $server->on('close',function ($serv,$fd)
    {
        echo "$fd"."关闭
    ";
    });
    $server->start();
    ?>
    • 客户端
    <?php
    
    $client = new swoole_client(SWOOLE_SOCK_TCP);
    if(!$client->connect('127.0.0.1',9501))
    {
        echo "连接失败
    ";
        exit;
    }
    
    //控制台提示输入:
    fwrite(STDOUT,"说点儿什么吧
    ");
    $msg = trim(fgets(STDIN));
    $client->send($msg);
    $ret = $client->recv();
    echo $ret;
    ?>
  • 相关阅读:
    golang变量2
    golang1
    golang api接收get,post请求读取内容方法
    docker2
    docker
    渗透1
    Crawley框架
    spider类
    爬豆瓣阅读遇到的问题
    CrawlSpiders类
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/10977338.html
Copyright © 2011-2022 走看看