Zend Exam Tips – Streamming

I will not only talk about the streamming question that I got on this latest Mock Exam, but as well something that I have noticed. The first time that I did this exam I went well, so the next exam wasn’t so hard and I went very well. This time that I took the exam, it was way harder than before and very close to what I got in the real Zend Certification Exam that I took last year, so as a tip, you should consider scoring Excellent in all topics of the Mock Exam so you can always get the most of the questions in the Mock Exam.

Now about the streamming, take a look at this question. It’s an essay type of question and it’s really not so hard, but it will get you confused.

 

Consider the following PHP script:

 

<?php

function get_socket($host, $port) {

  $fr = fsockopen($host, $port);

  stream_set_blocking($fr, false);

  return $fr;

}

 

// Assume $host1, $host2, etc are defined properly

$write_map[] = array(‘fr’ => get_socket($host1, $port1),

                     ‘data’ => str_pad(“”, 500000, “A”));

$write_map[] = array(‘fr’ => get_socket($host2, $port2),

                     ‘data’ => str_pad(“”, 500000, “B”));

$write_map[] = array(‘fr’ => get_socket($host3, $port3),

                     ‘data’ => str_pad(“”, 500000, “C”));

 

do {

  $write_sockets = array();

 

  foreach($write_map as $data) {

    $write_sockets[] = $data[‘fr’];

  }

 

  $num_returned = stream_select($r = null, $write_sockets, $e = null, 30);

 

  if($num_returned) {

    foreach($write_sockets as $fr) {

      foreach($write_map as $index => $data) {

        if($data[‘fr’] === $fr) {

          $len = fwrite($fr, $data[‘buf’]);

 

          if($len) {

            $data[‘buf’] = substr($data[‘buf’], $len);

 

            if(empty($data[‘buf’])) {

              fclose($data[‘fr’]);

              /* ????????? */

            }

          }

        }

      }

    }

  }

} while(count($write_map));

?>

What should go in the ??????? above for this script to function properly?

The answer for this question is simple: 

unset($write_map[$index]);

The answer by itself shows the logic that is behind the script and if you stop and read again the question you will see that this is really the only logic you can use. After the buffer has been written on the server, to avoid it continuing in an insane loop, you should unset the array in that index. Take a look at the while condition and you will understand.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

2 responses to “Zend Exam Tips – Streamming

  • john smith

    Hello mcloide,
    I am new in php and I am studying the ZCE certificate.Can you please guide me how to prepare stream, and socet.
    Regarding this question as you can see the option are
    (1) $num_returned > 0
    (2) $len > 0
    (3) !$empty($data[‘buf’])
    (4) count($write_socket)
    (5) count($write_map)
    so which one is the right option as per your suggestion my guess is (5) Am i right.

    Thanks

    • mcloide

      Hey John,

      Welcome to the blog. I didn’t understand fully your question so, can you explain it better so I can better help you.

      Besides the question, check out the Zend Exam series, also the get the Zend Certification Book. It does help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: