fsockopen example

$timeout,
                STREAM_CLIENT_CONNECT,
                $socket_context
            );
        } else {
            //Fall back to fsockopen which should work in more places, but is missing some features             $this->edebug(
                'Connection: stream_socket_client not available, falling back to fsockopen',
                self::DEBUG_CONNECTION
            );
            set_error_handler([$this, 'errorHandler']);
            $connection = fsockopen(
                $host,
                $port,
                $errno,
                $errstr,
                $timeout
            );
        }
        restore_error_handler();

        //Verify we connected properly         if (!is_resource($connection)) {
            
$url_parts = parse_url($url);
                $socket_host = $url_parts['host'];
                if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
                {
                    $socket_host = "ssl://$url_parts[host]";
                    $url_parts['port'] = 443;
                }
                if (!isset($url_parts['port']))
                {
                    $url_parts['port'] = 80;
                }
                $fp = @fsockopen($socket_host$url_parts['port']$errno$errstr$timeout);
                if (!$fp)
                {
                    $this->error = 'fsockopen error: ' . $errstr;
                    $this->success = false;
                }
                else
                {
                    stream_set_timeout($fp$timeout);
                    if (isset($url_parts['path']))
                    {
                        if (isset($url_parts['query']))
                        {

        if ($ssl == 'SSL') {
            $host = 'ssl://' . $host;
        }

        if ($port === null) {
            $port = $ssl === 'SSL' ? 993 : 143;
        }

        $errno  =  0;
        $errstr = '';
        $this->_socket = @fsockopen($host$port$errno$errstr, self::TIMEOUT_CONNECTION);
        if (!$this->_socket) {
            /** * @see Zend_Mail_Protocol_Exception */
            throw new Zend_Mail_Protocol_Exception('cannot connect to host; error = ' . $errstr .
                                                   ' (errno = ' . $errno . ' )');
        }

        if (!$this->_assumedNextLine('* OK')) {
            /** * @see Zend_Mail_Protocol_Exception */

  protected function findAvailablePort($host) {
    $port = 8888;
    while ($port >= 8888 && $port <= 9999) {
      $connection = @fsockopen($host$port);
      if (is_resource($connection)) {
        // Port is being used.         fclose($connection);
      }
      else {
        // Port is available.         return $port;
      }
      $port++;
    }
    return FALSE;
  }
$host = $this->proxy_host;
                $port = $this->proxy_port;
            }
        else
        {
            $host = $this->host;
            $port = $this->port;
        }

        $this->status = 0;

        if($fp = fsockopen(
                    $host,
                    $port,
                    $errno,
                    $errstr,
                    $this->_fp_timeout
                    ))
        {
            // socket connection succeeded
            return true;
        }
        

        $request .= $r;

        $request .= $xml;

        // Now send the request         if ($this->debug) {
            echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
        }

        if ($this->timeout) {
            $fp = @fsockopen($this->server, $this->port, $errno$errstr$this->timeout);
        } else {
            $fp = @fsockopen($this->server, $this->port, $errno$errstr);
        }
        if (!$fp) {
            $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
            return false;
        }
        fputs($fp$request);
        $contents = '';
        $debugContents = '';
        $gotFirstLine = false;
        

    if (!isset($port) || !$port) {$port = 110;}
        if(!empty($this->MAILSERVER))
            $server = $this->MAILSERVER;

        if(empty($server)){
            $this->ERROR = "POP3 connect: " . _("No server specified");
            unset($this->FP);
            return false;
        }

        $fp = @fsockopen("$server", $port$errno$errstr);

        if(!$fp) {
            $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
            unset($this->FP);
            return false;
        }

        socket_set_blocking($fp,-1);
        $this->update_timer();
        $reply = fgets($fp,$this->BUFFER);
        $reply = $this->strip_clf($reply);
        
trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE);
                            return;
                        }
                        break;
                    default:
                        trigger_error("[plugin] unrecognized attribute '".$param_key."'",E_USER_NOTICE);
                        return;
                }
            }
            if(!empty($proxy_host) && !empty($proxy_port)) {
                $_is_proxy = true;
                $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
            } else {
                $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
            }

            if(!$fp) {
                trigger_error("[plugin] unable to fetch: $errstr ($errno)",E_USER_NOTICE);
                return;
            } else {
                if($_is_proxy) {
                    fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
                } else {
                    


  /** * Checks whether a port is available. * * @param $port * A number between 1024 and 65536. * * @return bool */
  protected function checkPortIsAvailable($port) {
    $fp = @fsockopen(self::$hostName$port$errno$errstr, 1);
    // If fsockopen() fails to connect, probably nothing is listening.     // It could be a firewall but that's impossible to detect, so as a     // best guess let's return it as available.     if ($fp === FALSE) {
      return TRUE;
    }
    else {
      fclose($fp);
    }
    return FALSE;
  }

  
function _settimeout($sock) {
        if(!@stream_set_timeout($sock$this->_timeout)) {
            $this->PushError('_settimeout','socket set send timeout');
            $this->_quit();
            return FALSE;
        }
        return TRUE;
    }

    function _connect($host$port) {
        $this->SendMSG("Creating socket");
        $sock = @fsockopen($host$port$errno$errstr$this->_timeout);
        if (!$sock) {
            $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
            return FALSE;
        }
        $this->_connected=true;
        return $sock;
    }

    function _readmsg($fnction="_readmsg"){
        if(!$this->_connected) {
            $this->PushError($fnction, 'Connect first');
            
// Connection to port 465 should use implicit TLS (without STARTTLS)         // as per RFC 8314.         if ($this->SMTPPort === 465) {
            $ssl = 'tls://';
        }
        // But if $SMTPCrypto is set to `ssl`, SSL can be used.         if ($this->SMTPCrypto === 'ssl') {
            $ssl = 'ssl://';
        }

        $this->SMTPConnect = fsockopen(
            $ssl . $this->SMTPHost,
            $this->SMTPPort,
            $errno,
            $errstr,
            $this->SMTPTimeout
        );

        if (is_resource($this->SMTPConnect)) {
            $this->setErrorMessage(lang('Email.SMTPError', [$errno . ' ' . $errstr]));

            return false;
        }

        if ($ssl == 'SSL') {
            $host = 'ssl://' . $host;
        }

        if ($port === null) {
            $port = $ssl == 'SSL' ? 995 : 110;
        }

        $errno  =  0;
        $errstr = '';
        $this->_socket = @fsockopen($host$port$errno$errstr, self::TIMEOUT_CONNECTION);
        if (!$this->_socket) {
            /** * @see Zend_Mail_Protocol_Exception */
            throw new Zend_Mail_Protocol_Exception('cannot connect to host; error = ' . $errstr .
                                                   ' (errno = ' . $errno . ' )');
        }

        $welcome = $this->readResponse();

        strtok($welcome, '<');
        
Home | Imprint | This part of the site doesn't use cookies.