1
<?php

require_once('PEAR.php');

$GLOBALS['Net_EPP_Client_Version'] = '0.0.3';

class Net_EPP_Client {

    var $socket;

    function connect($host, $port=700, $timeout=1, $ssl=true, $sslcertpath, $sslpassphrase) {
        $context = @stream_context_create(); 
        $result = stream_context_set_option($context, 'ssl', 'allow_self_signed', 'true');
        $result = stream_context_set_option($context, 'ssl', 'local_cert', $sslcertpath); 
        $result = stream_context_set_option($context, 'ssl', 'passphrase', $sslpassphrase);

        $target = sprintf('%s://%s:%d', ($ssl === true ? 'ssl' : 'tcp'), $host, $port);
        if (!$this->socket = @stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context)) {
            return new PEAR_Error("Error connecting to $target - $errstr (code $errno)");
        } else {
            return $this->getFrame();
        }
    }

    function getFrame() {
        //stream_set_blocking($this->socket, 1);
        //stream_set_timeout($this->socket, 30);
        if (@feof($this->socket)) return new PEAR_Error('connection closed by remote server');

        //$hdr = @fread($this->socket, 4);
        $hdr = @fgets($this->socket, 5);
        //print_r($hdr); 
        //return new PEAR_Error($hdr);

        if (empty($hdr) && feof($this->socket)) {
            return new PEAR_Error('connection closed by remote server');

        } elseif (empty($hdr)) {
            //return new PEAR_Error('Error reading from server: '.$php_errormsg);
            $php_errormsg = isset($php_errormsg) ? $php_errormsg : "";
            return new PEAR_Error('Error reading from server: '. $php_errormsg);
        } else {
            /*
            $unpacked = unpack('N', $hdr);
            $answer = fread($this->socket, ($unpacked[1] - 4));
            return $answer;
            */
            $unpacked = unpack('N', $hdr);
            $length = $unpacked[1];

            if ($length < 5) {
                return new PEAR_Error(sprintf('Got a bad frame header length of %d bytes from server', $length));
            } else {

                //'|'.fread($this->socket, ($length - 4)).'|'; //not sure why, but this fixed some part here..
                //return '<'.fread($this->socket, ($length));
                return fread($this->socket, ($length));
            }
        }
    }


    function sendFrame($xml) {
        fwrite($this->socket, pack('N', (strlen($xml)+4)).$xml);
    }


    function request($xml) {
        $this->sendFrame($xml);
        return $this->getFrame();
    }

    function disconnect() {
        return @fclose($this->socket);
    }

}

?>

我遇到了一个问题,我将从这一行中获取内存泄漏

return fread($this->socket, ($length));

你能告诉我这条线有什么问题吗

if(!$this->socket = @stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context))

我搜索stream_socket_client()了服务器未关闭打开的连接的问题,套接字将保留来自服务器的数据。有什么解决办法吗?另一位软件工程师替换了这条线

return fread($this->socket, ($length));

到下面的代码。他们说他们不知道为什么会发生这个错误。

'|'.fread($this->socket, ($length - 4)).'|';
return '<'.fread($this->socket, ($length));

我想知道它是否有任何替代解决方案?

4

0 回答 0