PHP实现Socket服务器的代码

 更新时间:2008年04月03日 19:08:47   作者:  
PHP实现Socket服务器的代码
<?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_sockets.dll 打开

相关文章

  • php中根据某年第几天计算出日期年月日的代码

    php中根据某年第几天计算出日期年月日的代码

    在PHP中,使用内置的date()函数很容易得到任意一天是当前年的第几天,格式为date('z'),为此,很多PHP程序会用一年中的第几天作为数据库的索引(index)。
    2011-02-02
  • Yii 2.0中场景的使用教程

    Yii 2.0中场景的使用教程

    这篇文章主要给大家介绍了关于Yii 2.0中场景使用的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • 30 个很棒的PHP开源CMS内容管理系统小结

    30 个很棒的PHP开源CMS内容管理系统小结

    本文汇集了30个优秀的开源CMS建站系统,采用PHP开发。以下列表不分先后顺序
    2011-10-10
  • sphinx增量索引的一个问题

    sphinx增量索引的一个问题

    很早使用coreseek来实现对内容的搜索,并使用主索引+增量索引来实现新发的内容很快能搜索到,使用一直挺稳定。
    2011-06-06
  • Memcache 在PHP中的使用技巧

    Memcache 在PHP中的使用技巧

    Memcache 在PHP中的使用
    2010-02-02
  • PHP对象Object的概念 介绍

    PHP对象Object的概念 介绍

    类提供了一个基础,可以在此基础上创建实体(即这个类所建模的实体)的特定实例,这些特定实例称为对象(object)
    2012-06-06
  • PHP基于面向对象封装的分页类示例

    PHP基于面向对象封装的分页类示例

    这篇文章主要介绍了PHP基于面向对象封装的分页类,结合实例形式分析了php分页类针对页码判断、显示等操作的封装及分页类使用相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • php+mysqli批量查询多张表数据的方法

    php+mysqli批量查询多张表数据的方法

    这篇文章主要介绍了php+mysqli批量查询多张表数据的方法,涉及multi_query、store_result及more_results等函数的使用技巧,需要的朋友可以参考下
    2015-01-01
  • 浅析php-fpm静态和动态执行方式的比较

    浅析php-fpm静态和动态执行方式的比较

    这篇文章主要介绍了php-fpm静态和动态执行方式的比较,较为详细的分析了php-fpm静态和动态执行方式的原理、参数功能与相关使用技巧,需要的朋友可以参考下
    2016-11-11
  • PHP实现实时生成并下载超大数据量的EXCEL文件详解

    PHP实现实时生成并下载超大数据量的EXCEL文件详解

    EXCEL文件的处理是我们在日常工作中经常会遇到的,这篇文章主要给大家介绍了关于利用PHP如何实现实时生成并下载超大数据量的EXCEL文件,通过文中介绍的这个方法对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10

最新评论