php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21573 fsockopen does not block
Submitted: 2003-01-10 18:27 UTC Modified: 2003-01-11 07:04 UTC
From: ejthomas at adelphia dot net Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 4.3.0 OS: Windows XP
Private report: No CVE-ID: None
 [2003-01-10 18:27 UTC] ejthomas at adelphia dot net
I have code that would connect to a pop server and retrieve email messages, without using IMAP functions, which worked fine in PHP 4.0 to PHP 4.2.3, but no longer works correctly in PHP 4.3.0.  From what I can tell it seems that although the socket shows that it is in blocking mode, it acts as though it is not in blocking mode.  The following code demonstrates the problem.

With the debug statements added to the code and when it gets to the fgets function, it loops continuously, without reading data from the stream, until the time limit is reached.

<?php
$hostname = "pop-server";
$port = "110";
$timeout = 30;
global $errno, $errstr;

$fp = fsockopen ($hostname, $port, $errno, $errstr, $timeout);

// debug
print_r (socket_get_status ($fp));
echo "<br>\n";

socket_set_blocking ($fp, TRUE);

// debug
print_r (socket_get_status ($fp));
echo "<br>\n";

$continue = 1;
while ($continue)
{
  $chr = fgets ($fp, 1);
  $str .= $chr;

  // debug
  echo "chr = $chr<br>\n";
    
  $array = explode ("\r\n", $str);
  if ($str != $array[0])
  {
    $continue=0;
  }
}

fclose ($fp);
?>


Output from version 4.3.0:

Array ( [stream_type] => socket [unread_bytes] => 0 [timed_out] => [blocked] => 1 [eof] => ) 
Array ( [stream_type] => socket [unread_bytes] => 0 [timed_out] => [blocked] => 1 [eof] => ) 
chr =
chr =
chr =
.
. (Loops continuously)
.
chr =

Fatal error: Maximum execution time of 30 seconds exceeded in f:\httproot\popcorn0.8.2\includes\popcorn.inc on line 110


Output from version 4.2.3:

Array ( [timed_out] => [blocked] => 1 [eof] => [unread_bytes] => 0 ) 
Array ( [timed_out] => [blocked] => 1 [eof] => [unread_bytes] => 0 ) 
chr = +
chr = O
chr = K
chr = 
chr = C
chr = u
chr = b
chr = i
chr = c
...
chr = .
chr = n
chr = e
chr = t
chr = >
chr = 
chr = 


I realize that fsockopen, opens the stream in blocking mode by default, but I had to use socket_set_blocking prior to version 4.3.0 to get it to work properly.  I tried using stream_set_blocking with the same results.  Also, I have tried connecting to multiple POP mail servers with the same result.  Only when I go back to version 4.2.3, does it work properly.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-11 07:04 UTC] wez@php.net
Read the manual for fgets:

"
string fgets ( int fp [, int length])


Returns a string of up to length - 1 bytes read from the file pointed to by fp. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes.
"

In 4.2.x, fgets was not behaving as it should - we fixed this bug in 4.3.

Now, fgets() is the wrong function to use for what you are doing; you should be using fread() to read a single character.
If you insist on using fgets(), you need add one to the number of bytes to read. (fgets($fp, 2))

I'm marking this as bogus as it is really a problem in your script, and not a bug in PHP.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Apr 04 10:01:25 2020 UTC