PHP_socket解决方案(待测试) 发布日期:2017-03-03 01:30:31     博主推荐★

代码下载地址:http://pan.baidu.com/s/1dF9dEkL


服务器端 Server.php 

<?php 
  // 设置一些基本的变量 $host="127.0.0.1"; $port=1001;  
  // 设置超时时间 set_time_limit(0);  
  // 创建一个Socket 
  $socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");  
  //绑定Socket到端口 
  $result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n");  
  // 开始监听链接 
  $result=socket_listen($socket,3) or die("Could not set up socket listener\n"); 
  // 另一个Socket来处理通信 
  $spawn=socket_accept($socket) or die("Could not accept incoming connection\n");  
  // 获得客户端的输入 
  $input=socket_read($spawn,1024) or die("Could no read\n");  
  //向客户端写入响应信息 
  $output = 'Welcome,this is what you send:( '.$input.' ).'; 
  socket_write($spawn,$output,strlen($output)) or die("Could not write output\n");  
  // 关闭sockets 
  socket_close($spawn); socket_close($socket);
?>



客户端 Cilent.php 

<?php
 
header("Content-type:text/html;Charset=UTF-8");
class Socket {
 private $host;//连接socket的主机
 private $port;//socket的端口号
 private $error=array();
 private $socket=null;//socket的连接标识
 private $queryStr="";//发送的数据 
  
 public function __construct($host,$port) {
   if(!extension_loaded("sockets")){
     exit("请打开socket扩展 ");
   }
   if(empty($host))
    exit("请输入目标地址");
   if(empty($port))
    exit("请输入有效的端口号");
   $this->host=$host;
   $this->port=$port;
   $this->CreateSocket();//创建连接
 } 
 
 //创建socket
 private function CreateSocket(){
   !$this->socket&&$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//创建socket
   $r=@socket_connect($this->socket,$this->host,$this->port);
   if($r){
     return $r;   
   }else{
     $this->error[]=socket_last_error($this->socket);
     return false;
   }
 }
 
 //向socket服务器写入数据并读取
 public function eachMsg($contents){
   $this->queryStr="";
   $this->queryStr=$contents;
   !$this->socket&&$this->CreateSocket();
   $contents=$this->fliterSendData($contents);
   $result=socket_write($this->socket,$contents,strlen($contents));
   if(!intval($result)){
     $this->error[]=socket_last_error($this->socket);
     return false;
   }
   $response=socket_read($this->socket,12048);
   if(false===$response){
     $this->error[]=socket_last_error($this->socket);
     return false;
   }
   return $response;
 }
  
 //对发送的数据进行过滤
 private function fliterSendData($contents){
   //对写入的数据进行处理
   return $contents;
 }
 
 //所有错误信息
 public function getError(){
   return $this->error;
 }
 
 //最后一次错误信息
 public function getLastError(){
   return $this->error(count($this->error));
 }
 
 //获取最后一次发送的消息
 public function getLastMsg(){
   return $this->queryStr;
 }
 
 public function getHost(){
   return $this->host;
 }
 
 public function getPort(){
   return $this->port;
 }
 
 //关闭socket连接
 private function close(){
   $this->socket&&socket_close($this->socket);//关闭连接
   $this->socket=null;//连接资源初始化
 }
 
 public function __destruct(){
   $this->close();
 }
}

$SC = new Socket('127.0.0.1',1001); //打开Socket 连接
$REP = $SC->eachMsg('Hellow');      //发送信息并接收服务器返回的数据
var_dump($REP);
?>



首先执行服务器端文件,然后执行客户端文件,O了,就这么简单。

博文地址:https://blog.ahamu.cn/blog/detail.html?id=40
   
推荐文章
  • 1
    sysbench
    2020/07/08
  • 2
    phper转java记录篇-spring boot
    2020/06/10
  • 3
    thinkphp5.0使用路由之后,post请求的
    2020/05/19
  • 4
    springboot单元测试aop失效
    2020/05/15
  • 5
    脑海中的JVM
    2020/05/12
  • 6
    IDEA搜索插件时显示search results
    2020/05/12
  • 7
    spring boot 配置加载源码查找
    2020/04/20
  • 8
    通过javap命令分析java汇编指令
    2020/04/16
  • 9
    IDEA小知识:查看JVM内存使用情况的步骤
    2020/04/16
  • 10
    springboot-加载自定义的properti
    2020/04/14
  • 11
    Jenkins执行shell脚本无法启动子进程解决
    2020/04/03
  • 12
    mac idea激活找专业的
    2020/04/02
  • 13
    Jenkins + DockerSwarm 实现弹
    2020/03/31
  • 14
    mac swarm学习过程
    2020/03/31
  • 15
    spring cloud
    2020/03/18
  • 16
    JAVA开发中遇到的问题记录002
    2020/03/12
  • 17
    JAVA开发中遇到的问题记录001
    2020/03/07
  • 18
    php -i查看信息
    2020/02/23
  • 19
    phpStorm中使用xdebug工具调试dock
    2019/12/09
  • 20
    讲的比较好的B+树执行原理的文章
    2019/12/09
最喜标签
NYOJ 面试 AJAX ping CentOS 灰度算法 YII2