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中如何执行linux命令详解

    php中如何执行linux命令详解

    开发中遇到一种问题,需要在php函数中运行Linux系统代码,所以下面这篇文章主要给大家介绍了关于php中如何执行linux命令的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-11-11
  • php Notice: Undefined index 错误提示解决方法

    php Notice: Undefined index 错误提示解决方法

    字面意思就是未定义的索引,一般情况下是因为程序开发作者判断不严谨导致。一般不会影响程序的运行,具体的解决方法可以参考下。
    2010-08-08
  • php实现MD5加密16位(不要默认的32位)

    php实现MD5加密16位(不要默认的32位)

    今天做了个php链接mssql数据库,数据库中的表中字段使用MD5十六加密的。但是php中的MD5默认是32位,导致登录程序没办法使用md5加密跟表中字段匹配,在网上一搜也有不少人有这方面的困惑,后来找到一个解决办法,是正确的,就记录下来
    2013-08-08
  • php实现mysql连接池效果实现代码

    php实现mysql连接池效果实现代码

    这篇文章主要介绍了php代码实现mysql连接池效果,需要的朋友可以参考下
    2018-01-01
  • Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

    Fatal error: Allowed memory size of 134217728 bytes exhauste

    这篇文章主要介绍了Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)错误的解决方法,需要的朋友可以参考下
    2014-11-11
  • PHP5.5迭代生成器用法实例详解

    PHP5.5迭代生成器用法实例详解

    这篇文章主要介绍了PHP5.5迭代生成器用法,结合实例形式详细分析了PHP5.5迭代生成器的功能,定义及相关使用技巧,需要的朋友可以参考下
    2016-03-03
  • PHP使用反射机制实现查找类和方法的所在位置

    PHP使用反射机制实现查找类和方法的所在位置

    这篇文章主要介绍了PHP使用反射机制实现查找类和方法的所在位置,实例分析了PHP反射机制的原理与使用反射机制实现对类和方法的查找技巧,需要的朋友可以参考下
    2016-04-04
  • 关于Intype一些小问题的解决办法

    关于Intype一些小问题的解决办法

    Q:intype中文支持问题
    2008-03-03
  • 一道关于php变量引用的面试题

    一道关于php变量引用的面试题

    当一个变量等于另一个变量的引用的时候,这时任何一方改变了其值,另一方看到的这个值也会变化的。前加本次就表现出来,而后加下一次才会表现出来。
    2010-08-08
  • php实现给图片加灰色半透明效果的方法

    php实现给图片加灰色半透明效果的方法

    这篇文章主要介绍了php实现给图片加灰色半透明效果的方法,涉及对图像的操作,是非常实用的技巧,需要的朋友可以参考下
    2014-10-10

最新评论