Problem with blocking streams in PHP

Problem with blocking streams in PHP

am 07.02.2010 07:47:24 von Phani Raju

--=_affdaa02e3db8eb06c3b69fc9ff97cfd
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="UTF-8"

I am trying to open a blocking stream using fsockopen. I want to write and read XML input/output from a server. After opening the steam I will send a XML request to server using the stream. Once this request is sent, I want to keep listening on the port indefinitely.



I tried using blocking stream for this with a huge timeout. But had no success as it was not waiting for such a long period.



Code:

$parser = xml_parser_create("UTF-8");

if($stream = @fsockopen($host, $port, $errorno, $errorstr, $timeout)) {

stream_set_blocking($stream, 1);

stream_set_timeout($stream, TIMEOUT);

fwrite($stream, $xml."\n");

sleep(2);

while (!feof($stream)) {

$data = fread($stream, 1024);

xml_parse($parser, $data, feof($stream));

}

}



$xml has the xml to be sent. TIMEOUT is set to 3600*24 and $timeout is set to 300. As it is a blocking stream, till the data is not arrived, feof should not happen. But in this case, the stream is not waiting and returning eof. What is the problem with this code?



I tried using even stream_select but faced same issue.
--=_affdaa02e3db8eb06c3b69fc9ff97cfd--