note 17372 deleted from function.posix-mkfifo by jimw
| From: | jimw@php.net | Date: | Thu, 20 Dec 2001 21:28:23 +0000 |
| Subject: | note 17372 deleted from function.posix-mkfifo by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-23449@lists.php.net to get a copy of this message | ||
While playing around with fifo files I created the following class to make fifo files easier to
handle ... Thought it might serve as a good example...
fifo.php
<?
ob_implicit_flush();
class fifo {
var $fp;
var $to;
function fifo( $file, $mode ) {
if( !file_exists( $file ) ) {
print "creating pipe on fs\n";
if( !posix_mkfifo( $file, 0600 ) ) {
die( "could not create named pipe $file\n" );
}
}
print "opening fifo $file for $mode\n";
$this->fp = fopen( $file, $mode );
}
function read() {
// reads a line from a fifo file
$line = '';
do {
$c = fgetc( $this->fp );
if( ($c != '') and ($c != "\n") and !feof( $this->fp ) ) $line .= $c;
} while( ($c != '') and ($c != "\n") and !feof( $this->fp ) );
return $line;
}
function write( $data ) {
fputs( $this->fp, $data );
fflush( $this->fp );
}
}
The follwing two litte applications use the class above to illustrate basic client-server
communication ...
client.php
<?
include( 'fifo.php' );
$in = "/tmp/in";
$out = "/tmp/out";
$fin = new fifo( $in, 'r' );
$fout = new fifo( $out, 'w' );
print "said: Hello world\n";
$fout->write( "Hello World\n" );
print "got: " . $fin->read() . "\n";
print "said: Back at ya\n";
$fout->write( "Back at ya\n" );
print "got: " . $fin->read() ."\n";
?>
server.php
<?
include( 'fifo.php' );
$in = "/tmp/out";
$out = "/tmp/in";
$fout = new fifo( $out, 'w' );
$fin = new fifo( $in, 'r' );
print "got: " . $fin->read() ."\n";
print "said: Thanks, ... U2\n";
$fout->write( "Thanks, ... U2\n" );
print "got: " . $fin->read() ."\n";
?>