0

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?

Shafin Abrar
  • 471
  • 1
  • 5
  • 12
  • Probably some sort of firewall, that lets incoming requests on the default HTTP port 80 through by default, but blocks others. – 04FS Sep 18 '19 at 13:00
  • I also thought something similar, but not the incoming, it is blocking outgoing request on some port other than port 80. but how can i unblock that? – Shafin Abrar Sep 18 '19 at 13:23
  • It probably blocks based on the port of the target address you are trying to reach, not based on which port is used locally to make that outgoing request. – 04FS Sep 18 '19 at 13:28
  • I also think so, but how can I unblock it? – Shafin Abrar Sep 18 '19 at 13:36
  • Find out what firewall is running, and go check its documentation …? – 04FS Sep 18 '19 at 13:39

0 Answers0