Stream and Network Programming

WHEN IT COMES TO dealing with external data sources, PHP provides a great many different ways to communicating with the external world. These include facilities like file access and email management. However, both these systems are highly specialized: file management deals with your local filesystem, while the e-mail functions handle only a very narrow aspect of network communications. For more generic needs, PHP provides a facility called streams, which simply make it possible to treat any data source as a file. For example, the “fopen wrappers” that can be used to load up the contents of an external web page in your script are an excellent example of streams: they let you use file functions to pull content off the Internet. Finally, more complex operations can be managed through socket programming, which allows for the highest level of flexibility possible. This section of the exam tests your knowledge of these two areas of expertise.

15 cards   |   Total Attempts: 189
  

Cards In This Set

Front Back
Which of the following is not a valid PHP file wrapper resource? A. \\server\path\filename B./index.php C. myfile.txt D.compress.zlib://myfile.txt E. They all are valid
The correct answer is E-all of the items listed in the answers are completely valid wrapper resources in PHP. Almost all file-access functionality in PHP can now use any of these methods to work with both local and remote files.
What function can you use to create your own streams using the PHP stream wrappers and register them within PHP? Your Answer: ______________________
The stream_wrapper_register function is used to register a user-defined file wrapper (created as a PHP class) as a valid wrapper protocol. It takes two parameters, the name of the new protocol and a class name implementing it.
The Stream API provides all but which of the following pieces of information using the stream_get_meta_data function? A. Whether there is more data to be read B. Whether the stream has timed out or not C. Whether the stream is blocking D. How much data has passed through the stream E. The component parts the stream consists of
The correct answer is D. The stream_get_meta_data function does not tell you how much data has passed through the stream-it does, however, tell you how much there is left to be read.
Which of the following are valid PHP stream transports? (Choose 2) A. http B. STDIO C. ftp D. STDOUT E. stream
The correct answers are B and E. PHP only supports these two stream transports (STDIO for local operations and stream for remote operations) and will select the appropriate one automatically depending on the type of stream being created.
The stream context provides information about the data being transported over a given stream and can be used to pass configuration options to which of the following aspects of the stream? (Choose 2) A. Stream Filters B. Stream Transports C. File Wrappers D. The individual read / write streams E. All of the above
The correct answers are B and C. Stream contexts can be created and used to modify the behaviour of the file wrapper being used with a stream, or the transport of the stream itself. Usually, creating a stream context is not necessary, as PHP does a very good job of managing most common situations for you.
What function would you use to open a socket connection manually with the purpose of communicating with a server not supported by a file wrapper? Your Answer: ______________________
You would normally use the fsockopen function to open a socket connection for communicating with a server whose protocol is not supported by PHP. This is useful, say, for communicating with a server that uses its own protocol and can be combined with userdefined file wrappers to implement stream support in PHP.
Which of the following network transports doesn’t PHP support? A. tcp B. udp C. udg D. pdc E. unix
The correct answer is D-pdc, which isn’t a real network transport. On top of the other transports listed above, PHP also supports secure transports, such as ssl and tls.
Assume that you are attempting to communicate with a server that periodically sends data to you over the tcp network transport. The intervals at which this data is sent cannot be predicted, yet you must process it as soon as it arrives. Your script must also perform actions in between data transmissions from the server. When you write your script, you find that it often hangs on the call to fread() if the server takes too long to respond and your other actions aren’t being executed properly. How can this problem be fixed? A. Decrease max_execution_time, thereby forcing fread() to time out faster B. Decrease the timeout time of the connection when calling fsockopen() C. Turn off blocking on the socket D. Turn on blocking on the socket E. None of the above
The correct answer is C. By default, sockets created by the fsockopen function will have blocking enabled, which means any call to read/write data will “block” other code from being executed until the data is actually processed across the wire. With blocking turned off, if there is no data to read the call to fread() will simply return quickly and you will be free to move on to other things.
When dealing with timeout values in sockets, the connection timeout can be changed independently of the read/write time out. Which function must be used for this purpose? Your Answer: ______________________
To adjust the timeout of a socket when reading or writing data, you must use the stream_set_timeout function. It is not possible to adjust the timeout of read operations independently of writes-however, note that a stream timeout does not affect the connection timeout set when calling fsockopen().
Assume that you would like to write a script that reads plain-text data from an arbitrary stream and writes it back to a second stream ROT13-encoded. The encoding must be performed as you are writing to the second stream. What approach would be best suited for these purposes? A. Storing the encoded data in a temporary variable and then writing that variable to the stream B. Using stream filters to encode the data on-the-fly C. Creating a lookup table for ROT13, then encoding the data character by character on the fly as you write it. D. There is no way to encode in ROT13 on the fly E. None of the above
The correct answer is B. Stream filters can be applied to any stream and can be stacked to perform multiple manipulations on data flowing through the stream at once. For instance, one can add both a ROT13 filter and a base64 filter to the same stream to produce a combination base64/ROT13 encoding.
What will the following script output?
Answer D is correct. The ip2long function converts the string 127.0.256 into an integer representation of the valid IP 127.0.1.0, which is then outputted by long2ip(). This is actually an effective method of checking whether an IP is valid (as mentioned in the PHP Manual itself).
What will the following script do?
Answer C is correct. The getservbyname function returns the numeric port associated with a specific service and protocol—FTP and TCP in this case, which, normally run on port 21 (but not always, since you can edit your services file to change the port number).
What does the gethostbynamel function do? A. It returns the IP associated with a host name B. It returns a list of all the IPs associated with a host name C. It returns the IP associated with a host name using a long-integer representation D. It returns a list of all the IPs associated with a host name using a long-integer representation E. None of the above
Answer B is correct. gethostbynamel() returns an array containing all the IPs associated with a particular mnemonic address.
Which of the following operations cannot be performed using the standard ftp:// stream wrapper? (Choose 2) A. Reading a file B. Writing a file C. Establishing a stateful connection and changing directories interactively D. Creating a new directory
The correct answers are C and D. The ftp:// stream wrapper can be used to read and write data to or from an FTP server, but you cannot create a new directory or change the current directory interactively as you would from a normal FTP client with it.
How do you create a custom stream handler? A. By calling stream_wrapper_register() and defining a class to handle stream operations B. By registering a handler function with stream_wrapper_register() C. By creating a class that has the same name as the stream wrapper you want to use and then opening it with fopen() D. By loading the stream wrapper using stream_load()
Answer A is correct. The stream_wrapper_register function is used to register a new stream wrapper; it accepts the name of a class that will be used to control the stream’s functionality.