swoole 创建UDP服务器
udp_server.php
<?php
// 创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP
$serv = new swoole_server('127.0.0.1', 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); // 监听数据接收事件
$serv->on('Packet', function ($serv, $data, $clientInfo) {
$serv->sendto($clientInfo['address'], $clientInfo['port'], 'Server ' . $data);
var_dump($clientInfo);
}); // 启动服务器
$serv->start();
UDP服务器没有连接的概念,启动后客户端无需connect,直接可以向server监听的端口发送数据包。$clientInfo是客户端的相关信息
1.启动服务
$ /usr/local/php/bin/php udp_server.php
2.启动成功后,查看服务进程状态
$ ps aux | grep udp_server
oosten 0.0 2.2 pts/ Sl+ : : /usr/local/php/bin/php udp_server.php
oosten 0.0 0.4 pts/ S+ : : /usr/local/php/bin/php udp_server.php
oosten 0.0 0.7 pts/ S+ : : /usr/local/php/bin/php udp_server.php
3.nc连接服务器
$ nc -u 127.0.0.1 9502 ###-u,使用udp传输协议
hello
Server hello
4.客户端发送数据包后,服务端打印$clientInfo数据
array() {
["server_socket"]=>
int()
["server_port"]=>
int()
["address"]=>
string() "127.0.0.1"
["port"]=>
int()
}
5.结束进程
kill
参考:https://wiki.swoole.com/wiki
swoole 创建UDP服务器的更多相关文章
- node.js中通过dgram数据报模块创建UDP服务器和客户端
node.js中 dgram 模块提供了udp数据包的socket实现,可以方便的创建udp服务器和客户端. 一.创建UDP服务器和客户端 服务端: const dgram = require('dg ...
- Swoole学习(三)Swoole之UDP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...
- swoole 创建tcp服务器
server.php <?php /** * 创建tcp服务器 * Date: 2019/1/15 */ $serv = new swoole_server('127.0.0.1', 9501) ...
- swoole创建websocket服务器
目录 1 安装准备 1.1 安装swoole前必须保证系统已经安装了下列软件 1.2 下载并解压 1.3 编译安装成功后,修改php.ini 2 构建Swoole基本实例 2.1 tcp服务器实例 2 ...
- swoole 创建web服务器
http_server.php $http = new swoole_http_server("0.0.0.0", 9501); // 请求监听事件 $http->on('r ...
- Swoole 创建服务
1: 创建TCP 服务器 $serv = new swoole_server(‘127.0.0.1’,9501); 2:创建UDP服务器 $serv = new swoole_server('127 ...
- 【Swoole】简单安装与创建TCP服务器
pecl install swoole PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了php语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据 ...
- Swoole学习(二)Swoole之TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- socket | tcp客户端 tcp服务器 udp客户端 udp 服务器 创建方法
tcp服务器 #coding=utf-8 ''' 这里是tcp服务器端,要先启动 ''' import socket import threading bind_ip = "0.0.0.0& ...
随机推荐
- 导出excel设置样式(Aspose.Cells)
Aspose.Cells.Style style = xlBook.Styles[xlBook.Styles.Add()];style1.Pattern = Aspose.Cells.Backgrou ...
- SQL chema的新增和修改
1.先要创建你自己的schema create schema myschema 2. alter schema myschema transfer ado.User --执行完后,User表就 ...
- Failed to start NodeManager caused by "/var/lib/hadoop-yarn/yarn-nm-recovery/yarn-nm-state/LOCK: Permission denied"
Hadoop 安装步骤: 0. 安装前准备(节点机器,环境设置,yum源设置) 1. 配置并安装Cloudera-Manager 2. 启动 CM 服务 3. 安装CDH,并配置集群 4. 启动 ...
- pxe-kickstart
PXE client--->DHCP(pxelinux.0; next-server tftp-server) syslinux vmlinuz initrd.img ks.cfg--- ...
- (转)MyISAM Key Cache详解及优化
原文:http://huanghualiang.blog.51cto.com/6782683/1372721 一.MyISAM Key Cache详解: 为了最小化磁盘I/O,MyISAM将最频繁访问 ...
- MySql的数据目录
数据目录的位置 MySQL数据目录的默认位置已经被编译到MySQL服务器程序里了. 在启动服务器时,通过使用一个--datadir=dir_name选项可以明确指定数据目录位置.把MySQL数据目录安 ...
- [转]angularjs之ui-grid 使用详解
本文转自:http://blog.csdn.net/qhkabuqiluo/article/details/52237710 最近一段时间在使用angularjs 然后就找到ui-grid 这个比较不 ...
- vue中添加swiper轮播插件
网上找了很多,最后还是官网最完整. https://github.com/surmon-china/vue-awesome-swiper 安装: 1.npm install vue-awesome-s ...
- javascript下的arguments,caller,callee,call,apply示例及理解
(参考:http://justcoding.iteye.com/blog/589111) Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments ...
- MySQL批量插入多条数据方便测试
批量插入流程 数据库字段 delimiter create procedure doinsert3() begin declare i int; declare j int; ; ; ) do ins ...