PHP套接字问题-内存泄漏

PHP套接字问题-内存泄漏,php,stream-socket-client,Php,Stream Socket Client,你能告诉我这条线怎么了吗 <?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

你能告诉我这条线怎么了吗

<?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))
我搜索了一个问题,服务器没有关闭由
流\u套接字\u client()
打开的连接,套接字将保留来自服务器的数据。有什么解决办法吗?另一位软件工程师已经替换了这一行

<?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))
到下面的代码。他们说他们不知道为什么会发生这种错误

return fread($this->socket, ($length));
fread($this->socket,($length-4))。';
return“为什么你认为它在那一行中泄漏?嗨,我已经解决了这个问题。”。我引用了这个链接中的新代码。原因是套接字将被缓冲。所以内存会泄漏。谢谢收看。:)