php “无法打开插座”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3232639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Could not open socket"
提问by LightningWrist
How do I alleviate the "Could not open socket" error that is happening on my site?
如何缓解我网站上发生的“无法打开套接字”错误?
I have troubleshot that it is CAPTCHA(I'm using reCAPTCHA). It is only displaying this error on the two pages where I use reCAPTCHA.
我发现它是CAPTCHA(我正在使用reCAPTCHA)。它仅在我使用 reCAPTCHA 的两个页面上显示此错误。
I have been generating new sets of keys, and sometimes it works and sometimes it does not. For example, it worked on Safariand sometimes not, but on Firefox, and vice versa, and it worked for me and not for one of my partners and vice versa.
我一直在生成新的密钥集,有时有效,有时无效。例如,它在Safari 上有效,有时在Firefox 上有效,反之亦然,它对我有效,对我的合作伙伴之一无效,反之亦然。
How can I fix this problem? Could it be that my server is having trouble doing the fsocketopen command? If so, how do I fix that?
我该如何解决这个问题?可能是我的服务器在执行 fsocketopen 命令时遇到了问题?如果是这样,我该如何解决?
回答by Weston C
Could it be that my server is having trouble doing the fsocketopen command?
可能是我的服务器在执行 fsocketopen 命令时遇到了问题?
Exactly -- although it doesn't necessarily mean that something is wrong with your server. It just means that somewhere between your server and the recaptcha server, there's a network communications problem that prevents the socket connection from being opened.
没错——尽管这并不一定意味着您的服务器有问题。这只是意味着在您的服务器和 recaptcha 服务器之间的某个地方,存在阻止打开套接字连接的网络通信问题。
This could be a lot of things. It could be a config issue with your code or on your server, (particularly if there's some aspect of the configuration on your server that's dynamic), it could be an issue with the level of connectivity your server has, it could be a network config issue where your server is hosted, it could be a network configuration issue anywhere between your server and the recaptcha server, it could be a bandwidth issue where they're hosted, it could be a configuration issue on their side. You might want to use the extra error reporting arguments to fsockopento see if you can get any messages that make sense. You might also try your setup out on at least 2-3 different servers on totally different networks -- that could also give you a somewhat specific indication about where the problem is.
这可能是很多事情。这可能是您的代码或服务器上的配置问题(特别是如果您的服务器上的某些配置方面是动态的),这可能是您的服务器连接级别的问题,也可能是网络配置您的服务器托管的问题,这可能是您的服务器和 recaptcha 服务器之间的任何地方的网络配置问题,也可能是托管它们的带宽问题,也可能是他们这边的配置问题。您可能希望使用额外的错误报告参数fsockopen来查看是否可以获得任何有意义的消息。您还可以在完全不同的网络上的至少 2-3 台不同的服务器上尝试您的设置——这也可以为您提供有关问题所在的特定指示。
The other question, though, is how you're going to manage this kind of thing in general. fsockopenjust sometimes fails to get a connection, because in even the best configured network environment, there's no communications guarantee. Hardware fails, accidents happen, network admins have fat-finger moments, remote servers get confused, global thermonuclear war can take out a data center -- you just never know. So you've got to write your code (and manage your setup) so you've got fallback cases for when failure happens and you display error messages that are acceptable for the end user.
不过,另一个问题是您将如何管理此类事情。fsockopen只是有时无法获得连接,因为即使在配置最好的网络环境中,也无法保证通信。硬件出现故障,事故发生,网络管理员手忙脚乱,远程服务器混乱,全球热核War可能摧毁一个数据中心——你永远不知道。因此,您必须编写代码(并管理您的设置),以便在发生故障时获得回退案例,并显示最终用户可以接受的错误消息。
You might want to look into PHP's set_error_handlerfunction and set up a function to be called on occurrences where fsockopenfails. In some situations, I've become fond of using it to trigger exceptions, something like this:
您可能想要查看 PHP 的set_error_handler函数并设置一个函数,以便在发生fsockopen失败时调用。在某些情况下,我喜欢使用它来触发异常,例如:
function throw_error_exception($number = 0, $str = '',$file = null,$line = null) {
throw new ErrorException($str, 0, $number, $file, $line);
}
set_error_handler('throw_error_exception',E_ALL);
With that setup, you could manage fsockopenconnections something like this:
通过该设置,您可以fsockopen像这样管理连接:
try {
fsockopen('remote.host.com',8080,$fso_errnum,$fso_errstr,30);
} catch(Exception $e) {
// here you can look at properties/methods of $e, and $fso_* values, and
// figure out what nice error messages you want to display for your users
}

