note 40195 rejected from function.socket-create-pair by derek
| From: | derek@php.net | Date: | Tue, 06 Apr 2004 20:01:12 +0000 |
| Subject: | note 40195 rejected from function.socket-create-pair by derek | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-67731@lists.php.net to get a copy of this message | ||
Note Submitter: Ramon Herrera in Austin, TX
----
#!/usr/local/bin/php -q
<?
/* HERE'S A CORRECTED VERSION OF THE IPC EXAMPLE.
THE ORIGINAL CODE LACKED A COUPLE OF CLOSING PARENTHESES (AND NEWLINE CHARS FOR NICE OUTPUT).
*/
$ary = array();
$strone = "Message From Parent.";
$strtwo = "Message From Child.";
if(!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary))
echo socket_strerror(socket_last_error());
$pid = pcntl_fork();
if($pid == -1) {
echo "Could not fork Process.\n";
} elseif( $pid ) {
/*parent*/
socket_close($ary[0]);
if(!socket_write($ary[1], $strone, strlen($strone)))
echo socket_strerror(socket_last_error());
if(socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo)
echo "Received $strtwo\n";
socket_close($ary[1]);
} else {
/*child*/
socket_close($ary[1]);
if(!socket_write($ary[0], $strtwo, strlen($strtwo)))
echo socket_strerror(socket_last_error());
if(socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone)
echo "Received $strone\n";
socket_close($ary[0]);
}
usleep(1000); // THIS LETS THE SHELL PROMPT COME BACK CLEANLY.
?>