简体   繁体   English

警告:socket_connect()[function.socket-connect]:无法连接[110]:连接超时

[英]Warning: socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out

I have a php script using sockets and my code works well when I test it on localhost (XAMPP). 我有一个使用套接字的PHP脚本,当我在localhost(XAMPP)上测试时,我的代码运行良好。 But after I uploaded the same code to my web hosting, it doesn't work properly. 但在我将相同的代码上传到我的网络托管后,它无法正常工作。 In details, It loads a few minutes and finally gives out these error messages. 详细说明,它加载几分钟,最后给出这些错误消息。

Warning: socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out in /home/....

Warning: socket_send() [function.socket-send]: unable to write to socket [32]: Broken pipe in /home/....

Warning: socket_read() [function.socket-read]: unable to read from socket [107]: Transport endpoint is not connected in /home/....

I think it is probably because the server blocks the socket connections. 我认为这可能是因为服务器阻塞了套接字连接。 And my questions are: 我的问题是:

  1. Can I execute some code to check whether the server blocks it or not? 我可以执行一些代码来检查服务器是否阻止它? ( eg phpinfo() ) (例如phpinfo())
  2. What server configurations should I focus on so that I can try to make it connect? 我应该关注哪些服务器配置,以便我可以尝试连接它? (I can access .htaccess file but not php.ini) (我可以访问.htaccess文件,但不能访问php.ini)
  3. Is SSL necessary for socket connections? 套接字连接需要SSL吗?
  4. If I finally cannot fix this problem because of the server settings, I need to have another web hosting. 如果由于服务器设置我终于无法解决这个问题,我需要另外一个虚拟主机。 What features/keywords on the 'plan list' should I notice? 我应该注意“计划清单”中的哪些功能/关键字?

I know there are quite many questions but I hope someone can help me. 我知道有很多问题,但我希望有人可以帮助我。 Thanks very very much if anyone can give me some advice. 非常感谢,如果有人能给我一些建议。

This is weird... As far as I know, there's actually no specific INI directive to enable/disable socket connections. 这很奇怪......据我所知,实际上没有特定的INI指令来启用/禁用套接字连接。 There's a directive to set the timeout (default_socket_timeout), but I doubt it could change anything. 有一个指令来设置超时(default_socket_timeout),但我怀疑它可能会改变任何东西。

SSL has nothing to do with the sockets. SSL与套接字无关。 Unless you are using a protocol that may rely on SSL (eg. HTTP). 除非您使用的协议可能依赖于SSL(例如HTTP)。

It is more likely that the server TCP configuration is preventing access to some TCP ports on remote hosts. 服务器TCP配置更有可能阻止访问远程主机上的某些TCP端口。 It is usually the case on shared hosting plans where security and resource usage are tighter. 通常情况下,共享主机方案的安全性和资源使用更为严格。

If you perhaps find a way to connect to a standard port (say 80), you'll find if you have to deal with your hosting provider or go elsewhere :) 如果您可能找到一种连接到标准端口(比如80)的方法,您会发现是否必须与您的托管服务提供商打交道或去其他地方:)

Try to execute the following code on your host. 尝试在主机上执行以下代码。 It may help finding if this issue is related to the network configuration 它可能有助于查找此问题是否与网络配置有关

//just in case
if (!extension_loaded('sockets')) {
    die('The sockets extension is not loaded.');
}

echo '<p><strong>Establishing connection...</strong></p>';
$socket = socket_create(AF_INET,SOCK_STREAM,0);
if (!socket_connect($socket, "stackoverflow.com", 80))
{
    die('Socket error : '.socket_strerror(socket_last_error()));
}

echo '<p><strong>Connection successful!</strong></p>';

$request = join("\n",array(
    "GET / HTTP/1.1",
    "Connection: close",
    "Host: stackoverflow.com",
    "User-Agent: Mozilla/5.0 (Windows NT 6.1)",
    "Accept: text/html,*/*;q=0.8",
    ""));
socket_write($socket,$request,strlen($request));

$response = socket_read($socket,2048);


echo "<p><strong>This is the received data : </strong></p>";
echo '<pre>'.htmlentities($response).'</pre>';

socket_close($socket);
$options = array( 'http' => array(

    'user_agent'    => 'toixen',        // who am i
    'max_redirects' => 10,              // stop after 10 redirects
    'timeout'       => 2,               // timeout on response
    ) );


$context = stream_context_create( $options );

$url = "http://toixen_1:123@btcguild.com:8332";  
$dh = fopen( "$url",'r',false,$context);

fwrite($dh, "post");

$result = fread($dh,8192);                                                                                                                             
echo $result

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM