Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out

Tags:

php

sockets

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.

like image 743
Timespace7 Avatar asked Dec 28 '25 16:12

Timespace7


1 Answers

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);
like image 159
ibtarek Avatar answered Dec 31 '25 18:12

ibtarek