PHP实现Socket服务器的代码


Posted in PHP onApril 03, 2008

<?php
ob_implicit_flush();
set_time_limit(0);

$address = "192.40.7.93";//换成你自己的地址
$port = 10000;

if(($socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) == false)
 echo "错误(socket_create):".socket_strerror(socket_last_error())."<br />";

if(socket_bind($socket,$address,$port) == false)
 echo "错误(socket_bind):".socket_strerror(socket_last_error())."<br />";

if(socket_listen($socket) == false)
 echo "错误(socket_listen):".socket_strerror(socket_last_error())."<br />";

/*
After the socket socket has been created using socket_create() and bound to a name with socket_bind(), 
it may be told to listen for incoming connections on socket. 
*/

while(true){
 if(($msgSocket = socket_accept($socket)) == false){
  echo "错误(socket_accept):".socket_strerror(socket_last_error())."<br />";
  break;
 }

 /*
 this function will accept incoming connections on that socket. 
 Once a successful connection is made, a new socket resource is returned, which may be used for communication. 
 If there are multiple connections queued on the socket, the first will be used. 
 If there are no pending connections, socket_accept() will block until a connection becomes present. 
 If socket has been made non-blocking using socket_set_blocking() or socket_set_nonblock(), FALSE will be returned. 
 */

 $msg = "Welcome!<br />";
 //socket_write($msg,$msg,strlen($msg));
 $command = "";

 while(true){
  if(($buf = socket_read($msgSocket,2048,PHP_BINARY_READ)) == false){
   echo "错误(socket_read):".socket_strerror(socket_last_error())."<br />";
   break 2;
  }

  /*
  The function socket_read() reads from the socket resource socket created by the socket_create() or socket_accept() functions. 
  The maximum number of bytes read is specified by the length parameter. 
  Otherwise you can use \r, \n, or \0 to end reading (depending on the type parameter, see below).   
  */

  /*
  if(!$buf = trim($buf))
   continue; // ????

  if($buf == "quit")
   break;

  if($buf == "shutdown"){
   socket_close($msgSocket);
   break 2;
  }

  $tallBack = "You say:$buf\n";
  socket_write($msgSocket,$tallBack,strlen($tallBack));
  */

  if(ord($buf) != 13)
   $command .= $buf;
  else{
   $command1 = "You Say:$command\r\n";
   socket_write($msgSocket,$command1,strlen($command1));
   echo "User typed:".$command."<br />";
   $command = "";
  }
 }
 socket_close($msgSocket);
}

socket_close($socket);
?>

 

然后打开CMD,输入:telnet 192.40.7.93 10000,自己体验去吧!
PHP实现Socket服务器的代码
注,要把:php_sockets.dll 打开

PHP 相关文章推荐
实用函数9
Nov 08 PHP
php 无限级 SelectTree 类
May 19 PHP
PHP HTML代码串 截取实现代码
Jun 29 PHP
PHP的变量总结 新手推荐
Apr 18 PHP
如何利用php array_multisort函数 对数据库结果进行复杂排序
Jun 08 PHP
PHP 快速排序算法详解
Nov 10 PHP
PHP使用trim函数去除字符串左右空格及特殊字符实例
Jan 07 PHP
thinkphp5 URL和路由的功能详解与实例
Dec 26 PHP
ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
Apr 02 PHP
提高Laravel应用性能方法详解
Jun 24 PHP
PHP保留两位小数的几种方法
Jul 24 PHP
laravel5环境隐藏index.php后缀(apache)的方法
Oct 12 PHP
mysql+php分页类(已测)
Mar 31 #PHP
PHP 数字左侧自动补0
Mar 31 #PHP
加强版phplib的DB类
Mar 31 #PHP
PHP截取汉字乱码问题解决方法mb_substr函数的应用
Mar 30 #PHP
PHP5中的时间相差8小时的解决办法
Mar 28 #PHP
php heredoc和phpwind的模板技术使用方法小结
Mar 28 #PHP
WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.3.0配置
Mar 28 #PHP
You might like
php win下Socket方式发邮件类
2009/08/21 PHP
解析php中两种缩放图片的函数,为图片添加水印
2013/06/14 PHP
与文件上传有关的php配置参数总结
2013/06/14 PHP
实现在同一方法中获取当前方法中新赋值的session值解决方法
2014/06/26 PHP
跟我学Laravel之路由
2014/10/15 PHP
PHP利用百度ai实现文本和图片审核
2019/05/08 PHP
PHP xpath提取网页数据内容代码解析
2020/07/16 PHP
使用javascript将时间转换成今天,昨天,前天等格式
2015/06/25 Javascript
VUEJS实战之构建基础并渲染出列表(1)
2016/06/13 Javascript
移动端滑动插件Swipe教程
2016/10/16 Javascript
用户管理的设计_jquery的ajax实现二级联动效果
2017/07/13 jQuery
Vue工程模板文件 webpack打包配置方法
2017/12/26 Javascript
JS中获取 DOM 元素的绝对位置实例详解
2018/04/23 Javascript
详解Vue 多级组件透传新方法provide/inject
2018/05/09 Javascript
微信小程序支付前端源码
2018/08/29 Javascript
解决vue-router在同一个路由下切换,取不到变化的路由参数问题
2018/09/01 Javascript
vue2 设置router-view默认路径的实例
2018/09/20 Javascript
vue写h5页面的方法总结
2019/02/12 Javascript
Vue之beforeEach非登录不能访问的实现(代码亲测)
2019/07/18 Javascript
python正则表达式抓取成语网站
2013/11/20 Python
使用Python获取Linux系统的各种信息
2014/07/10 Python
Python多进程multiprocessing用法实例分析
2017/08/18 Python
基于python实现在excel中读取与生成随机数写入excel中
2018/01/04 Python
python+flask实现API的方法
2018/11/21 Python
如何基于python实现脚本加密
2019/12/28 Python
python ffmpeg任意提取视频帧的方法
2020/02/21 Python
Python ckeditor富文本编辑器代码实例解析
2020/06/22 Python
python自动化测试三部曲之request+django实现接口测试
2020/10/07 Python
Linux管理员面试经常问道的相关命令
2014/12/12 面试题
小学生自我鉴定
2013/10/12 职场文书
实习自荐信
2013/10/13 职场文书
2014年体育部工作总结
2014/11/13 职场文书
黑暗中的舞者观后感
2015/06/18 职场文书
2016教师六五普法学习心得体会
2016/01/21 职场文书
90行Python代码开发个人云盘应用
2021/04/20 Python
JPA 通过Specification如何实现复杂查询
2021/11/23 Java/Android