douzhonglong3789 2016-11-05 18:01
浏览 65
已采纳

连接到另一台机器的反应服务器

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

$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
";

$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("
Test for '$ip'
");
    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?

  • 写回答

1条回答 默认 最新

  • dongxinche1264 2016-11-06 16:52
    关注

    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".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式