14.9 Reading and Writing I/O Vectors
TechniqueUse 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); ?> CommentsUsing 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. |