ID: 42497
User updated by: donald at designknights dot com
-Summary: php hangs when using fgets with a socket
Reported By: donald at designknights dot com
Status: Open
Bug Type: Network related
Operating System: SUSE linux 10.1
PHP Version: 5.2.4
New Comment:
i have stream_set_blocking = 1 and used stream_get_line instead of
fgets
this seems to have solved the race condition.
Previous Comments:
------------------------------------------------------------------------
[2007-08-31 15:51:30] donald at designknights dot com
Description:
------------
this is a script to connect to a pop3 server and parse emails.
$connection = fsockopen($popServer, $port, $error ,$string ,$timeout);
$response = fgets($connection, $buffer);
$newLine = "\r\n";
$localhost = "www.designknights.com";
if(empty($connection)) {
echo "Failed to connect: $response";
exit;
}
else {
$logArray['connection'] = "Connected to: $response";
}
echo "sending username<br>";
ob_flush();
flush();
fputs($connection, "USER $username". $newLine);
$response = fgets($connection, $buffer);
$logArray['userresponse'] = "$response";
fputs($connection, "PASS $password". $newLine);
$response = fgets($connection, $buffer);
$logArray['passresponse'] = "$response";
if (substr($response,0,3)!="+OK"){
echo "Mail server said: ".$response;
exit;
}
fputs($connection,"LIST".$newLine);
$msg_array = array();
$response = fgets($connection,$buffer);
$logArray['listresponse'] = "$response";
$messages = explode(" ",$response);
$nummessages = $messages['1'];
echo "number of messages =".$nummessages;
stream_set_blocking($connection,1);
for ($i=1;$i<=$nummessages;$i++){
fputs($connection,"retr ".$i.$newLine,$buffer);
$message = '';
$line = '';
//This loop is where it hangs
while (!feof($connection){
$line = fgets($connection,$buffer);
echo "<br> line = $line";
ob_flush();
flush();
}
$message_blown = explode(":",$message);
echo "<br> message $i = <br>";
print_r($message_blown);
}
echo "return = ".print_r($messages)."<br>";
print_r($logArray);
fclose($connection);
Reproduce code:
---------------
See above
Expected result:
----------------
It should to my understanding put the lines into the var names $message
then explode that message into $message_blown using : as a separator.
Actual result:
--------------
it hangs and the max execution time kills php5 while running the
script.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42497&edit=1