PHP套接字问题 - 内存泄漏

时间:2012-09-18 03:33:35

标签: 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 = @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));

我想知道它是否有其他解决方案?

0 个答案:

没有答案