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

警告: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 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加