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:
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.
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("\n",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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With