【发布时间】:2017-02-12 11:50:29
【问题描述】:
我的要求是在 AWS EC2 中托管一个 php 套接字服务器脚本。并与该套接字服务器通信。这是我到目前为止所做的,但它不起作用。
server.php 充当套接字服务器并监听客户端的脚本:
<?php
// set some variables
$host = "127.0.0.1";
$port = 53;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
在 AWS 安全组中,我添加了以下入站规则:
- 我使用命令“php server.php”从 SSH 运行服务器、php 脚本。
- 我尝试从输入以下内容的 SocketTest 工具连接到服务器 价值
host:52.xx.xx.xxx(此处掩盖了实际值)和port:53
我无法连接到套接字服务器。如果我在这里遗漏任何东西,如果有人可以指导我,那将非常有帮助
请注意,在本地 xampp 服务器中测试时,相同的 server.php 程序运行良好
【问题讨论】:
-
连接是否超时?如果您接受安全组中所有端口上的所有入站协议,问题是否仍然存在?
-
连接超时。你能帮我看看如何接受安全组中所有端口上的所有入站协议吗?
-
安全组 -> 选择您的安全组 -> 单击编辑 -> 类型:所有流量 -> 协议:全部 -> 端口范围:0-65535 -> 来源:Anywhere 和 0.0.0.0/0 .当然,请记住它仅用于调试。你不应该在生产中设置它。
-
我添加了新的入站规则。现在连接被拒绝
-
您确定
SocketTest(可能是什么?)正在建立UDP连接而不是TCP连接?
标签: php sockets amazon-web-services amazon-ec2 udp