• 技术文章 >php教程 >PHP源码

    很简单的一个socket客户端类

    PHP中文网PHP中文网2016-05-25 17:15:09原创369
    //socke操作类
    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 wr($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();
    	}
    }

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php数据库连接类 下一篇:MVC的调度器和模板类

    相关文章推荐

    • php学习笔记之面向对象编程• php 日期与日间之差函数• php 站点使用XML文件做配置类• PHP禁止图片文件的被盗链函数• php实现连接access数据库并转txt写入的方法_php技巧

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网