Socket takes a long time to 'finish'
| From: | Alvar Saenz-Otero | Date: | Wed, 29 Aug 2007 08:40:37 +0000 |
| Subject: | Socket takes a long time to 'finish' | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-261441@lists.php.net to get a copy of this message | ||
Hello,
I have a small but somewhat annoying issue.
Whenever I open a socket to another server, the file is read very quickly, but it takes the up to 30 seconds or so to close the connection...
Here is what I do:
$sever = "server.mit.edu";
$url = "/link";
$port = 80;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$server_protocol = $_SERVER['SERVER_PROTOCOL'];
$valsencoded = "";
while (list($key,$value) = each ($content))
$valsencoded .= urlencode($key) . "=" . urlencode($value) ."&";$valsencoded = substr($urlencoded,0,-1); $content_length = strlen($valsencoded); $headers = "POST $url $server_protocol Content-Type: application/x-www-form-urlencoded Host: $server Content-Length: $content_length "; // open socket $socket = fsockopen($server, $port, $errno, $errstr, 60.0); if (!$socket) {
$error = "ERROR: cannot contact Mailman server";
return array(false,$error,$error); }
if ($errstr) {
$error = "ERROR: fSockOpen Error ($errno): $errstr";
return array(false,$error,$error); }
fputs($socket, $headers); // send headers
fputs($socket, $urlencoded); // send post data
// "tried stream_set_timeout($socket,3)" here
// get the returned page
while (!feof($socket))
{
$buf = @fgets($socket, 2048);
$htmlreturned .= $buf;
}
fclose($socket);
If I put a debug 'echo "BUF: $buf\n" inside the while loop, I see the returned html from the post show up pretty much immediately, within about a second, maybe less.
However, the process 'stalls' after that for between 15-30 seconds, and then ends correctly.
I have tried to use timeouts, but they did not have any effects (did not really expect them to, but just in case).
My setup is Debian 4.0, php 5.2.0-8+etch7. Unfortunately I don't know the other server OS's/applications.
I found similar questions in posts from 2002 and 2003, but there were no replies (2002-04-30 guido.d.berger and 2003-11-25 thomas weber).
Any ideas greatly appreciated,
Alvar