使PHP socket_connect超时 [英] Make PHP socket_connect timeout

查看:396
本文介绍了使PHP socket_connect超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小应用程序来分析网络连接.它从浏览器运行,并连接到本地PHP/Apache服务器.然后,它要求PHP通过原始套接字发送ping数据包.问题是,如果我尝试ping的主机不存在或无法响应ping,我们将永远无法从服务器得到答复.

I have a small application I created to analyze the network connection. It runs from a browser and connects to a local PHP/Apache server. It then asks PHP to send a ping packet through a raw socket. THe problem is that if the host I am trying to ping isn't alive or won't answer to pings, we never get an answer from the server.

我相信套接字请求将一直存在,直到重新启动apache为止.我最近从我的应用程序中得到的结果是混合的,我指责Apache使用了太多的套接字.目前,我已经设置了AJAX呼叫的超时时间,对此我感到很满意.但是我真的需要让PHP进行超时,这样我才不会为无法访问的主机打开500,000个套接字.

I beleave the socket request lives until apache is restarted. I have been getting mixed results from my application lately and I am blaming apache using too many sockets. Currently I have set the AJAX call's timeout and I was happy with it. But I really need to make PHP do the timeouting so that I won't have 500,000 sockets open to an unreachable host.

一些示例代码:

$sockconn = @socket_connect($socket, $target, null);
if(!$sockconn)
{
    $raw['error'] = socket_strerror(socket_last_error());
    $raw['status'] = false;
    return $raw;
}

这是不会超时的功能.我需要让它超时.另外,PHP脚本的执行时间不会影响套接字.

This is the function that won't timeout. I need to get it to timeout. Also PHP script execution time DOES NOT affect sockets.

我一无所知.

推荐答案

您可以使用以下或者,您可以使用无阻塞套接字并定期轮询套接字以查看远程主机是否响应.

Alternatively, you can use non-blocking sockets and periodically poll the socket to see if the remote host responded.

这篇关于使PHP socket_connect超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆