skurty
skurty

Reputation: 739

Unable to connect with fsockopen in PHP (Connection timed out)

I developed an application for a friend and he recently changed his dedicated server for overload problems.

Since this migration, a part of the site doesn't work anymore: a connection on a socket in PHP.

In this part, I had to establish a connection with another server so I realized a function to open a socket:

function openSocket($host, $port) {
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
    
    if (!$fp) {
        return 0;
    }
    
    fputs($fp, $envoi);
    
    return $fp;
}

In the code, I use this function like that:

$sock = openSocket($host, $port);

while (!feof($sock)) {
    echo fread($sock, 8192);
}

fclose($sock);

Sometimes, I need to open the socket for several minutes and since the migration, my friend has this error in his log:

PHP Warning: fsockopen(): unable to connect to ...:80 (Connection timed out) in ..., referer: ...

I searched if there was a specific configuration in PHP / Apache but I found nothing.

I tried to increase the last parameters of fsockopen or the stream_set_timeout() function but nothing worked.

Thank you for your help

Upvotes: 1

Views: 8493

Answers (3)

Bhavik Shah
Bhavik Shah

Reputation: 311

Even i was facing same problem.
I was developing a site for my client.
He wanted me to set the time from the dedicated time servers.
So, i tried to establish a connection to that server using UDP. Tried a lot but failed. Long after that i found that some port was closed which was required to be open for establishing connection.
Checkout the port required and then confirm that the respective port is opened

Upvotes: 0

foogames
foogames

Reputation: 39

Some hosting providers and dedicated server setups limit allowed outgoing connections in their firewall settings. This is especially useful to prevent unusual bot or exploit scripts from establishing connections FROM the server should it be compromised. Check if there are any egress/outgoing firewall rules that might be blocking you.

Other reasons - an ingress/inbound firewall rule on the remote server is denying your connection attempt, or highly latent connectivity between the machines.

Upvotes: 0

plenderj
plenderj

Reputation: 573

Is there a reliable connection between the two computers?

Upvotes: 1

Related Questions