reactphp / socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP.

Home Page:https://reactphp.org/socket/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connection refused error

wolrd opened this issue · comments

commented

Hello!

I can not send a command to external server. If I send it inside the local server, then it works correctly.

The port is open on an external server.

What could be the problem?

My code:

$loop = Factory::create();
        $connector = new Connector($loop);
        $connector
            ->connect(sprintf('%s:%s', '35.158.187.551', '9090'))
            ->then(
                function(ConnectionInterface $conn) use ($params)
                {
                    $messageBody = json_encode($params);

                    $conn->write($messageBody);
                    $conn->end();
                },
                function ($error)
                {
                    var_dump($error);
                    if (stripos($error, 'Connection refused') == false)
                        \Log::info($error);
                }
            );
        $loop->run();

My server:

$loop = Factory::create();
		$socket = new \React\Socket\Server(sprintf('%s:%s', $host, $port), $loop);
        $socket = new LimitingServer($socket, null);
       

		$socket->on('connection', function(ConnectionInterface $connection) use ($socket)
		{
		    $connection->on('data', function($data) use ($connection, $socket)
            {
...
            }
           });
});

		$loop->run();

Error:

object(RuntimeException)#502 (7) { ["message":protected]=> string(18) "Connection refused" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(54) "/var/www/html/vendor/react/socket/src/TcpConnector.php" ["line":protected]=> int(110) ["trace":"Exception":private]=> array(71) { [0]=> array(4) { ["function"]=> string(22) "React\Socket\{closure}" ["class"]=> string(25) "React\Socket\TcpConnector" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> resource(466) of type (Unknown) [1]=> object(React\EventLoop\StreamSelectLoop)#458 (8) { ["nextTickQueue":"React\EventLoop\StreamSelectLoop":private]=> object(React\EventLoop\Tick\NextTickQueue)#453 (2) { ["eventLoop":"React\EventLoop\Tick\NextTickQueue":private]=> *RECURSION* 
...

HI @wolrd, thanks for reporting!

The "connection refused" error message means the server that you're trying to connect to actively rejected the connection to the given port. This usually means the IP address is valid and this host is up and running, but there's no socket open on the given port.

You can verify this with any other client application that opens a TCP/IP connection to the given address, for example using telnet with telnet example.com 80.

On top of this, your example uses the invalid IP address 35.158.187.551 (note the last octet). Perhaps it's just a matter of fixing this copypasta?

I hope this helps! 👍

commented

@clue I showed not the real IP address.
I did not understand what it means "but there is no socket open on the given port."
I have a ReactPHP Server running on this server and it works. It also receives requests, but inside the server.

What should I do?

commented

@clue This is the real IP address http://35.158.187.551/ . We have closed access to directories through Apache. It can not affect the work of the library? After all, requests are not proxied.

commented

@wolrd if you can receive requests locally then make sure that you're listening on 0.0.0.0 instead of 127.0.0.1 or localhost to accept external connections.

commented

@andig
How can I do this?

Code my server:

$loop = Factory::create();
		$socket = new \React\Socket\Server(sprintf('%s:%s', '127.0.0.1', '9090'), $loop);
        $socket = new LimitingServer($socket, null);
       

		$socket->on('connection', function(ConnectionInterface $connection) use ($socket)
		{
		    $connection->on('data', function($data) use ($connection, $socket)
            {
...
            }
           });
});

		$loop->run();
commented

Replace 127.0.0.1 with 0.0.0.0

commented

@andig thank you! It works.

And why does this work?

commented

Because previously your socket was only listening on the loopback interface. Now it is doing so on the actual external network interface(s).

commented

@andig thanks!