user1032531 user1032531 - 1 month ago 8
HTTP Question

Connecting to react server from another machine

Following the instructions at http://reactphp.org/, I created a server:

<?php
require 'vendor/autoload.php';
$app = function ($request, $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello World\n");
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();


I then started it on a Linux box with an IP of 192.168.1.200, and it appears to be running:

[Michael@devserver react]$ php example.php
Server running at http://127.0.0.1:1337

[root@devserver react]# netstat -lnp | grep 1337
tcp 0 0 127.0.0.1:1337 0.0.0.0:* LISTEN 25984/php


To test it, they say to place http://127.0.0.1:1337/ in a browser, but the machine doesn't have a browser. As an alternative, I used cURL, and it is in fact running, but it can only be accessed as
127.0.0.1
(I've turned off iptables for testing, but no help).

<?php
function test($ip)
{
$ch = curl_init();
curl_setopt_array( $ch, [CURLOPT_URL=>$ip,CURLOPT_RETURNTRANSFER=>true,CURLOPT_PORT=>1337] );
echo("\nTest for '$ip'\n");
echo var_dump(curl_exec( $ch ));
}
test('127.0.0.1');
test('192.168.1.200');
test('192.168.1.201');


Output:

Test for '127.0.0.1'
string(12) "Hello World
"

Test for '192.168.1.200'
bool(false)

Test for '192.168.1.201'
bool(false)


ifconfig
shows the following:

[root@devserver ~]# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:282 errors:0 dropped:0 overruns:0 frame:0
TX packets:282 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:54330 (53.0 KiB) TX bytes:54330 (53.0 KiB)

wlan0 Link encap:Ethernet HWaddr B8:76:3F:69:31:95
inet addr:192.168.1.201 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::ba76:3fff:fe69:3195/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:26783 errors:0 dropped:0 overruns:0 frame:0
TX packets:22265 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3599653 (3.4 MiB) TX bytes:5331824 (5.0 MiB)

[root@devserver ~]#


How can I connect to this react server from another machine?

Answer

The second param to the $socket->listen(1337); line is defaulted to '127.0.0.1'. You could try putting in the actual IP of the correct interface so that it binds to that one instead (ie change it to $socket->listen(1337,'192.168.1.201');)

However that assumes your IP will never change. If you would like the server to accept connections via 127.0.0.1 AND your external ip (Basically accept connections from any interface), then you will want to use `$socket->listen(1337,'0.0.0.0'); This is basically a special IP that represent "all interfaces on the local machine".