Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not open socket"

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.

I have been generating new sets of keys, and sometimes it works and sometimes it does not. For example, it worked on Safari and sometimes not, but on Firefox, and vice versa, and it worked for me and not for one of my partners and vice versa.

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?

like image 206
LightningWrist Avatar asked Jul 12 '10 22:07

LightningWrist


People also ask

What does socket open mean?

To put things simply, a Socket that is open is a socket that is either waiting for connection or has successfully connected with another Socket . When a socket has been closed, it means that this socket is no longer available for connection, and that it's resources has already been released.

What causes socket error?

Socket errors can be caused by various issues including connectivity problems on the network, client or server computers or due to a firewall, antivirus or a proxy server. This error occurs when the socket connection to the remote server is denied.

What is a socket connect error?

Resolution. If you are getting a socket connect error when downloading the content filter database you may have a corrupt SSL client. The error message suggests that the process responsible for performing the download was unable to access the ssl-client data structure in memory.


1 Answers

Could it be that my server is having trouble doing the fsocketopen command?

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.

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 fsockopen to 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.

The other question, though, is how you're going to manage this kind of thing in general. fsockopen just 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.

You might want to look into PHP's set_error_handler function and set up a function to be called on occurrences where fsockopen fails. In some situations, I've become fond of using it to trigger exceptions, something like this:

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 fsockopen connections something like this:

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
}
like image 168
Weston C Avatar answered Sep 21 '22 14:09

Weston C