dongshi1606 2012-09-16 12:20
浏览 717
已采纳

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

I have a php script using sockets and my code works well when I test it on 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? ( e.g. 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)
  3. Is SSL necessary for socket connections?
  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.

  • 写回答

2条回答 默认 最新

  • douyinghuo8874 2012-09-16 13:39
    关注

    This is weird... As far as I know, there's actually no specific INI directive to enable/disable socket connections. There's a directive to set the timeout (default_socket_timeout), but I doubt it could change anything.

    SSL has nothing to do with the sockets. Unless you are using a protocol that may rely on SSL (eg. HTTP).

    It is more likely that the server TCP configuration is preventing access to some TCP ports on remote hosts. 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 :)

    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("
    ",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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现