Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,092
    Thanks
    15
    Thanked 1 Time in 1 Post

    Unhappy Zend PHP 5 Certification: Exam Question Assistance (20 May 2010)

    Hey,

    I have came across the following Q in a PHP Zend mock exam:


    Consider the following PHP script:

    PHP Code:
    <?php
    function get_socket($host$port) {
      
    $fr fsockopen($host$port);
      
    stream_set_blocking($frfalse);
      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 null30);

      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?

    There was no multiple choice answers given, just a textarea to type your answer in (for those familiar with it).

    Cheers,
    Picco

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
          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']); 
                  unset(
    $write_map[$index]);
                } 
              } 
            } 
    This says, foreach of the $key => $value pairs on $write_map array, which is an array of sockets and is dynamic, write $data['buf'] as if $data['fr'] is equal to this socket. As long as fwrite writes data (not false), buffer more data to the $data['buf'] which is the remaining data between $data['buf'] and the length written. Once empty, we close the stream.
    This process will occur as long as $write_map has one or more items in it. As soon as you close the socket you can no longer write to it, which will cause an error. Removing it from the $write_map will prevent it from trying this again.

    I cannot be 100% certain if this works to tell you the truth. Without being able to test this, I can only speculate on what appears to be the case. However, I also assume that the /* ???? */ is to indicate only 1 line of instructions is necessary, and the unset appears to be the proper one.

    Can anyone please confirm?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,092
    Thanks
    15
    Thanked 1 Time in 1 Post
    Thanks for this Fou-Lu, yes the question only requires one line of code for the answer.

    Can anyone else confirm Fou-Lu's answer (i'm sure it's spot on though).

    Thanks,
    Picco


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •