PHP套接字不听

时间:2013-05-19 06:06:51

标签: php sockets

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

打开并关闭它。在网络服务器上,我运行脚本,除了“倾听连接”

,我什么也看不见

1 个答案:

答案 0 :(得分:1)

我认为这是关于许可问题:

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

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