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

13,932

Solution 1

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);

Solution 2

$options = array( 'http' => array(

    'user_agent'    => 'toixen',        // who am i
    'max_redirects' => 10,              // stop after 10 redirects
    'timeout'       => 2,               // timeout on response
    ) );


$context = stream_context_create( $options );

$url = "http://toixen_1:[email protected]:8332";  
$dh = fopen( "$url",'r',false,$context);

fwrite($dh, "post");

$result = fread($dh,8192);                                                                                                                             
echo $result
Share:
13,932
Timespace
Author by

Timespace

Updated on June 05, 2022

Comments

  • Timespace
    Timespace almost 2 years

    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.

  • Timespace
    Timespace over 11 years
    I ran your code on my host and it displays 'Connection successful!' but it gives same error message if I changed the address and port to my targeted server. Is there any ways to check whether which port on remote hosts are blocked by my host? Thank you for your answer!
  • ibtarek
    ibtarek over 11 years
    could you please let me know more about these errors? To know if a TCP port is blocked, you have to ask the system administrator. The server may be configured to silently dump packets for some ports so you get a timeout.
  • Timespace
    Timespace over 11 years
    The error message is as same as above. i.e. socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out My targeted server have a port 5222 and I cannot change this. I have asked my server administrator and he/she tells me only some TCP ports are allowed for web hosting service, due to security reasons. If the server administrator doesn't change the configurations, what tricks I can do so that I can connect to port 5222?
  • ibtarek
    ibtarek over 11 years
    I'm afraid you don't have much of a choice if the sysadmin is blocking incoming AND outgoing traffic on this port.There's nothing to do at the PHP level to circumvent it. You'd better get another hosting solution that suits your needs.