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

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集