我的要求是在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安全组中,我添加了以下入站规则:
主机:52.xx.xx.xxx(此处屏蔽了实际值)和端口:53
我无法连接到套接字服务器。如果有人可以指导我,如果我在这里遗漏任何东西,那将非常有帮助
请注意,在本地xampp服务器
中测试时,相同的server.php程序工作正常答案 0 :(得分:0)
您使用了&#39; 127.0.0.1&#39;作为服务器脚本中的主机。 您必须将其替换为您服务器的私有IP地址。