Logo Search packages:      
Sourcecode: egroupware-egw-pear version File versions  Download package

Net_Socket::connect ( addr,
port = 0,
persistent = null,
timeout = null,
options = null 
)

Connect to the specified port. If called when the socket is already connected, it disconnects and connects again.

Parameters:
string $addr IP address or host name.
integer $port TCP port number.
boolean $persistent (optional) Whether the connection is persistent (kept open between requests by the web server).
integer $timeout (optional) How long to wait for data.
array $options See options for stream_context_create.
public

Returns:
boolean | PEAR_Error True on success or a PEAR_Error on failure.

Definition at line 97 of file Socket.php.

References setBlocking().

    {
        if (is_resource($this->fp)) {
            @fclose($this->fp);
            $this->fp = null;
        }

        if (!$addr) {
            return $this->raiseError('$addr cannot be empty');
        } elseif (strspn($addr, '.0123456789') == strlen($addr) ||
                  strstr($addr, '/') !== false) {
            $this->addr = $addr;
        } else {
            $this->addr = @gethostbyname($addr);
        }

        $this->port = $port % 65536;

        if ($persistent !== null) {
            $this->persistent = $persistent;
        }

        if ($timeout !== null) {
            $this->timeout = $timeout;
        }

        $errno = 0;
        $errstr = '';
        if ($options && function_exists('stream_socket_client')) {
            if ($this->timeout) {
                $timeout = $this->timeout;
            } else {
                $timeout = 0;
            }

            $context = stream_context_create($options);
            $flags = ($this->persistent === true ? STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT : STREAM_CLIENT_CONNECT);
            $fp = @stream_socket_client($this->addr.':'.$this->port, $errno, $errstr, $timeout, $flags, $context);
        } else {
            $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
            if ($this->timeout) {
                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
            } else {
                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
            }
        }

        if (!$fp) {
            return $this->raiseError($errstr, $errno);
        }

        $this->fp = $fp;

        return $this->setBlocking($this->blocking);
    }


Generated by  Doxygen 1.6.0   Back to index