亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

php創建udp server client demo

標簽:
PHP

作为无连接的通信协议,udp的开销要比tcp要小而且速度会更快

server

<?phpnamespace App\Shell;use demaya\Console\Shell;class LogServer extends Shell{   protected $addr = '127.0.0.1';   protected $port = 10000;   public function start()
   {       if (($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) == FALSE) {
           $errorcode = socket_last_error();
           $errormsg = socket_strerror($errorcode);           $this->error("创建socekt失败: [$errorcode] $errormsg");
       }       $this->success('socket 创建成功...');       // 绑定到 ip 端口
       if (!socket_bind($socket, $this->addr, $this->port)) {
           $errorcode = socket_last_error();
           $errormsg = socket_strerror($errorcode);           $this->error("bind socket失败: [$errorcode] $errormsg");
       }       $this->success('socket bind成功...');       while (true) {           $this->info("Waiting for data ... \n");           //Receive 
           $r = socket_recvfrom($socket, $buf, 512, 0, $remote_ip, $remote_port);           $this->info("$remote_ip : $remote_port -- " . $buf);           //Send back
           socket_sendto($socket, "OK " . $buf, 100, 0, $remote_ip, $remote_port);
       }
       socket_close($socket);
   }

}
<?phpnamespace App\Shell;use demaya\Console\Shell;class LogClient extends Shell{    protected $addr = '127.0.0.1';    protected $port = 10000;    public function start()
    {        if (($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) == FALSE) {
            $errorcode = socket_last_error();
            $errormsg = socket_strerror($errorcode);            $this->error("创建socekt失败: [$errorcode] $errormsg");
        }        $this->success('socket 创建成功...');        while (true) {
            $input = $this->climate->input('Enter a message to send :');
            $input = $input->prompt();            if (!socket_sendto($socket, $input, strlen($input), 0, $this->addr, $this->port)) {
                $errorcode = socket_last_error();
                $errormsg = socket_strerror($errorcode);                $this->error("Could not send data: [$errorcode] $errormsg \n");
            }            
            if (socket_recv($socket, $reply, 2045, MSG_WAITALL) === FALSE) {
                $errorcode = socket_last_error();
                $errormsg = socket_strerror($errorcode);                $this->error("Could not receive data: [$errorcode] $errormsg \n");
            }            $this->info("Reply : $reply");
        }
        socket_close($socket);
    }

}

说明

webp

udp server

webp

udp client

此案例代码为我自己构建的框架demaya上测试,以上为运行结果

  • udp 通信没有经过三次握手,是不可靠的通信,应用场景应该 充分考量

  • 我用来建立一个日志收集server 所有系统统一往这个server上发送日志



作者:麦田348462402
链接:https://www.jianshu.com/p/097463d08664


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消