php - Connecting to react server from another machine -
following instructions @ http://reactphp.org/, created 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 @ http://127.0.0.1:1337\n"; $socket->listen(1337); $loop->run();
i started on linux box ip of 192.168.1.200, , appears running:
[michael@devserver react]$ php example.php server running @ 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, place http://127.0.0.1:1337/ in browser, machine doesn't have browser. alternative, used curl, , in fact running, can accessed 127.0.0.1
(i've turned off iptables testing, no help).
<?php function test($ip) { $ch = curl_init(); curl_setopt_array( $ch, [curlopt_url=>$ip,curlopt_returntransfer=>true,curlopt_port=>1337] ); echo("\ntest '$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 '127.0.0.1' string(12) "hello world " test '192.168.1.200' bool(false) test '192.168.1.201' bool(false)
ifconfig
shows 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 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 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 connect react server machine?
the second param $socket->listen(1337);
line defaulted '127.0.0.1'. try putting in actual ip of correct interface binds 1 instead (ie change $socket->listen(1337,'192.168.1.201');
)
however assumes ip never change. if server accept connections via 127.0.0.1 , external ip (basically accept connections interface), want use `$socket->listen(1337,'0.0.0.0'); special ip represent "all interfaces on local machine".
Comments
Post a Comment