Socket封装

<?php
// +----------------------------------------------------------------------
// | 新增udp传输
// +----------------------------------------------------------------------
// | author: bruce<376394074@qq.com>
// +----------------------------------------------------------------------
//

class Socket {
    protected $_config = array(
        'persistent'    => false,
        'host'            => 'localhost',
        'protocol'        => 'tcp',
        'port'            => 80,
        'timeout'        => 30
    );

    public $config = array();
    public $connection = null;
    public $connected = false;
    public $error = array();

    public function __construct($config = array()) {
        $this->config    =    array_merge($this->_config,$config);
        //echo getprotobyname($this->config['protocol']);
        if (!is_numeric($this->config['protocol'])) {
        //    $this->config['protocol'] = getprotobyname($this->config['protocol']);
        }
    }

    public function connect() {
        if ($this->connection != null) {
            $this->disconnect();
        }

        if ($this->config['persistent'] == true) {
            $tmp = null;
            if($this->config['protocol'] == 'tcp'){
                $this->connection = @pfsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
            }else{
                $this->connection = @pfsockopen("udp://".$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
            }
        } else {
            if($this->config['protocol'] == 'udp'){
                $this->connection = fsockopen("udp://".$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
            }else{
                $this->connection = fsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
            }
        }

        if (!empty($errNum) || !empty($errStr)) {
            $this->error($errStr, $errNum);
        }

        $this->connected = is_resource($this->connection);

        return $this->connected;
    }

    public function error() {
    }

    public function write($data) {
        if (!$this->connected) {
            if (!$this->connect()) {
                return false;
            }
        }
        return fwrite($this->connection, $data, strlen($data));
    }

    public function writeByte($data, $len) {
        if (!$this->connected) {
            if (!$this->connect()) {
                return false;
            }
        }
        return fwrite($this->connection, $data, $len);
    }


    public function read($length=1024) {
        if (!$this->connected) {
            if (!$this->connect()) {
                return false;
            }
        }

        if (!feof($this->connection)) {
            return fread($this->connection, $length);
        } else {
            return false;
        }
        $this->disconnect();
    }
/**
 * 关闭连接
 */
    public function disconnect() {
        if (!is_resource($this->connection)) {
            $this->connected = false;
            return true;
        }
        $this->connected = !fclose($this->connection);

        if (!$this->connected) {
            $this->connection = null;
        }
        return !$this->connected;
    }

    public function __destruct() {
        $this->disconnect();
    }

}

转载于:https://my.oschina.net/zhuyaxiong/blog/278383

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值