14.9 Reading and Writing IO Vectors


14.9 Reading and Writing I/O Vectors

You want to read and write I/O vectors with PHP.

Technique

Use the readv() and writev () functions along with the vector manipulation functions:

 <?php $vector1 = build_iovec(5, 40, 40, 30, 40, 20); $vector2 = build_iovec(2, strlen($data1), strlen($data2)); set_iovec($vector2, 1, $data1); set_iovec($vector2, 2, $data2); $sock1 = socket(AF_INET, SOCK_STREAM, 0); if ($sock1 < 0)     die(strerror($sock1)); $sock2 = socket(AF_INET, SOCK_STREAM, 0); if ($sock2 < 0)     die(strerror($sock2)); if (($ret = bind($sock1, $addr1, $port1)) < 0)     die(strerror($ret)); if (($ret = connect($sock2, $addr2, $port2)) < 0)     die(strerror($ret)); if (($ret = listen($sock1, 5)) < 0)     die(strerror($ret)); while (readv($sock1, $vector1) >= 0)     writev($sock2, $vector2); ?> 

Comments

Using vectors is almost the same as using the read() and write() functions, except that I/O vectors enable you to provide named buffers to specify where to read and where data should be written. You must also use I/O vectors with the recvmsg() and the sendmsg () functions.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net