Re: Socket takes a long time to 'finish'
| From: | Richard Lynch | Date: | Wed, 29 Aug 2007 19:08:44 +0000 |
| Subject: | Re: Socket takes a long time to 'finish' | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-261490@lists.php.net to get a copy of this message | ||
Try just leaving out the fclose($socket) and let PHP close it at the
end for you...
It's probably not IDEAL, but it may at least get you moving forward
with other bigger issues.
Check the http://bugs.php.net/ bugs as well -- Might be something
in
there of use. There will be a few zillion bogus ones for sockets
though... :-(
On Wed, August 29, 2007 3:40 am, Alvar Saenz-Otero wrote:
> 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Please vote for this great band:
http://acl.mp3.com/feature/soundandjury/?band=COMPANY-OF-THIEVES
Requires email confirmation.
One vote per day per email limit.
Obvious ballot-stuffing will be revoked.