You are here

function Net_Socket::connect in Flickr API 5

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

@access public

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.:

Return value

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

File

phpFlickr/PEAR/Net/Socket.php, line 96

Class

Net_Socket
Generalized Socket class.

Code

function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) {
  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;
  }
  $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
  $errno = 0;
  $errstr = '';
  if ($options && function_exists('stream_context_create')) {
    if ($this->timeout) {
      $timeout = $this->timeout;
    }
    else {
      $timeout = 0;
    }
    $context = stream_context_create($options);
    $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
  }
  else {
    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);
}