2
include 'config.php';
set_time_limit(0);
//sending message
$msgOkay = "okay";
$msg1 = "1";
$msg2 = "2";
$msg3 = "3";
$msg4 = "4";
$msg5 = "5";
$msg6 = "6";

echo "Starting Listening Master. \n";

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))){
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Failed to create socket: [$errorcode] $errormsg \n");
}
echo "Socket created! \n";
socket_bind($sock, 0, 899);
echo "Socket Bound \n";
while (true) {

socket_listen($sock, 100);
echo "Listening for connections \n";
$client = socket_accept($sock);
echo "Connection Established. \n";
if(socket_getpeername($client , $address, $port)){
    echo "Client $address : $port has successfully established connection with socket master. \n";
}

$input = socket_read($client);
$request = explode("\n", $input);
if($request[0] == "dovote"){
    $key = $request[1];
    $id = $request[2];
    $player = $request[3];
    $ip = $request[4];



}

elseif($request[0] == "test"){
    $key = $request[1];
    $id = $request[2];
    $player = $request[3];
    $ip = $request[4];  

    $QueryTest = ("SELECT * FROM socketmanager WHERE privatekey = '".$key."' AND id = '".$id."'");
    $resultTest = mysql_query($QueryTest);
    if(mysql_num_rows($resultAuth) == "1"){
        socket_send($sock, $msg1, strlen($msg1), 0);
    }
    else{
        socket_send($sock, $msg3, strlen($msg3), 0);
    }

}

else{
    if(!socket_send($sock, $msg3, strnlen($msg3), 0)){
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not send data: [$errorcode] $errormsg \n");
    }
}



}

我已经将它上传到我的网络服务器,这是一个 VPS,我可以控制一切(端口 899 是开放的)。而当我在我的 Mac 上运行时:

telnet
open ip 80

它打开它并关闭它。在网络服务器上,我运行脚本,除了“正在侦听连接”之外,我什么也没看到

4

1 回答 1

1

我认为这是关于权限问题:

Non-root user can't bind port below 1024.
SELinux limitation or firewall problems.

您应该检查 socket_bind() 和 socket_listen() 返回 TRUE 或 FALSE,然后深入调试。

于 2013-08-25T07:45:23.913 回答