php套接字服务器仅适用于localhost但不适用于实时服务器

时间:2017-11-20 22:48:59

标签: php sockets server

我有托管域godaddy和我启用套接字模块,但无法正常工作 此代码仅在localhost中工作,当我在服务器上传它不起作用。

代码server.php

<?php
// set some variables
$host = '150.113.178.20';
$port = 5000;

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

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Bind the source address
if( !socket_bind($sock, $host , $port) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not bind socket : [$errorcode] $errormsg \n");
}

echo "Socket bind OK \n";

if(!socket_listen ($sock , 10))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not listen on socket : [$errorcode] $errormsg \n");
}

echo "Socket listen OK \n";

echo "Waiting for incoming connections... \n";

//start loop to listen for incoming connections
while (true) 
{
    //Accept incoming connection - This is a blocking call
    $client =  socket_accept($sock);

    //display information about the client who is connected
    if(socket_getpeername($client , $address , $port))
    {
        echo "Client $address : $port is now connected to us. \n";
    }

    //read data from the incoming socket
    $input = socket_read($client, 1024000);

    $response = "OK .. $input";

    // Display output  back to client
    socket_write($client, $response);
}

当我在ssh中执行server.php脚本时没问题

enter image description here

但是当我从CMD写信时:

enter image description here

错误日志文件

enter image description here

1 个答案:

答案 0 :(得分:0)

经过一番研究,我发现代码是正确的 Godaddy不会打开自定义端口。 代码不起作用,因为与GoDaddy(或其他托管网站)具有不同的安全策略localhost。