简体   繁体   English

php套接字服务器仅在localhost上有效,而在实时服务器上不可用

[英]php socket server works only on localhost but not live server

I have hosting domain godaddy and i enabled socket module, but dose not work this code works only in localhost when i upload it in server it does not work. 我有托管域godaddy并启用了套接字模块,但是无法正常工作,当我在服务器中上载它时,此代码仅在localhost中有效。

code server.php 代码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);
}

when i execute server.php script in ssh no problem 当我在ssh中执行server.php脚本时没问题

在此处输入图片说明

but when i write from CMD : 但是当我从CMD写信时:

在此处输入图片说明

Error log file 错误日志文件

在此处输入图片说明

After some research i found that The code is correct Godaddy does not open custom ports. 经过一番研究,我发现该代码是正确的Godaddy不会打开自定义端口。 The code does not work because have a different security policy localhost than GoDaddy (or other hosting website). 该代码不起作用,因为其安全策略本地主机与GoDaddy(或其他托管网站)不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP套接字服务器在本地主机上工作,但在实时服务器上不工作 - php socket server works on localhost but not on live server php: @fopen 在 localhost 中工作,但在实时服务器中无效 - php: @fopen works in localhost but not in live server phpmailer可在localhost上运行,但不能在实时服务器上运行 - phpmailer works on localhost but not on live server 路由在localhost上有效,但在实时服务器上无效 - Routing works on localhost, but not on live server php fwrite仅适用于本地主机而不适用于服务器? - php fwrite only works on localhost not server? php 代码在 xampp 本地主机上运行良好,但在实时服务器上运行不正常 - php code works fine on xampp localhost but not working on live server 使用Wordpress的PHP文件下载可在localhost上运行,但不能在实时服务器上运行 - PHP file download with Wordpress works on localhost but not live server PHP Cookies 在本地主机上运行良好,但在实时服务器上不起作用 - PHP Cookies works well on localhost, but it's not working on live server PHP - file_get_contents() 在 localhost 但在实时服务器上完美运行 - PHP - file_get_contents() works perfectly on localhost but on live server Php脚本可以在localhost中完全运行,而只有一部分可以在服务器上运行 - Php Script works fully in localhost and only part works on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM