Why php stream_socket_client is not connecting to port other than port 80?

1.1k Views Asked by At

I have a server hosted on ipaddress:8728. I can access it's service perfectly using my Java Client program. Now I am trying to access this service from my codeigniter web app hosted on the web using stream_socket_client(). But it returns Error 111(Connection Refused). Then I moved my server to port 80(ipaddress:80) and stream_socket_client program is working fine. Why does stream socket client fails to fetch data from from server on port other than port 80 and when my java socket client is working perfectly fine.

Bellow is my PHP code.

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;
}

Is it happening for mistake in my code? or some protective feature on the server where web app is hosted?

0

There are 0 best solutions below