首页 > 解决方案 > 为什么 php stream_socket_client 没有连接到端口 80 以外的端口?

问题描述

我有一个服务器托管在ipaddress:8728. 我可以使用我的 Java 客户端程序完美地访问它的服务。现在我正在尝试从托管在 Web 上的 codeigniter Web 应用程序访问此服务,使用stream_socket_client(). 但它返回错误 111(拒绝连接)。然后我将我的服务器移动到端口 80( ipaddress:80) 并且stream_socket_client程序运行正常。为什么流套接字客户端无法从端口 80 以外的端口上的服务器获取数据,并且当我的 Java 套接字客户端工作正常时。

贝娄是我的 PHP 代码。

public function connect($ip, $login, $password)
{
    for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
        $this->connected = false;
        $PROTOCOL = ($this->ssl ? 'ssl://' : '' );
        $context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
        $this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
        $this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);//this line has exception
        if ($this->socket) {
            echo "\nSocket is not null";
            socket_set_timeout($this->socket, $this->timeout);
            $this->write('/login', false);
            $this->write('=name=' . $login, false);
            $this->write('=password=' . $password);
            $RESPONSE = $this->read(false);
            if (isset($RESPONSE[0])) {
                if ($RESPONSE[0] == '!done') {
                    if (!isset($RESPONSE[1])) {
                        // Login method post-v6.43
                        $this->connected = true;
                        break;
                    } else {
                        // Login method pre-v6.43
                        $MATCHES = array();
                        if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
                            if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
                                $this->write('/login', false);
                                $this->write('=name=' . $login, false);
                                $this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
                                $RESPONSE = $this->read(false);
                                if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
                                    $this->connected = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            fclose($this->socket);
        }else{
            echo "||\nError no = ".$this->error_no."||";
            echo "||\n Error Message = ".$this->error_str."||";
            echo "||\nsocket is null||";
        }


        sleep($this->delay);
    }
    if ($this->connected) {
        $this->debug('Connected...');
    } else {
        $this->debug('Error...');
    }
    return $this->connected;
}

是因为我的代码中的错误而发生的吗?或托管 Web 应用程序的服务器上的某些保护功能?

标签: phprestcodeigniterstream-socket-client

解决方案


推荐阅读