Jump to content

[SOLVED] Sending to and receiving from named pipes


benjumanji

Recommended Posts

I'm sure there is quite an obvious flaw in my php :( I have a process that I want to pipe text to, and then receive text back from. I know the process that I am piping to and receiving from works, because I knocked up a perl script to do what I want php to do and everything worked just hunky-dory. It seems to be hanging at the fread command, I know my process is getting the text from php, and firing a reply back but the script isn't receiving it. Here is my php code:

 

<?php
$pipe_there=fopen('/tmp/my_pipe','r+');
fwrite($pipe_there,'voila du text');
fclose($pipe_there);
$pipe_back=fopen('/tmp/my_other_pipe','r+');
$output=fread($pipe_back,1000);
fclose($pipe_back);
?>

 

Any help would be greatly appreciated.

does my_other_pipe contain any text?

 

Yes, at least, I'm as sure as I can be that it does. Here is a perl script that I made that does what I want php to do:

 

#!/usr/bin/perl
use Fcntl;
$input="some random text\n";
sysopen(FIFO,"/tmp/my_pipe", O_WRONLY);
print FIFO $input;
close FIFO;
print "input sent : $input";
sysopen(FIFO,"/tmp/my_other_pipe", O_RDONLY);
@output=<FIFO>;
close FIFO;
print  @output;

 

If I run this, it will pipe text down /tmp/my_pipe and get a response down /tmp/my_other_pipe. I'm confused..

You're opening the file in read-only mode the first time I lied:

<?php
$pipe_there=fopen('/tmp/my_pipe','w');
fwrite($pipe_there,'voila du text');
fclose($pipe_there);
$pipe_back=fopen('/tmp/my_other_pipe','r');
$output=fread($pipe_back,1000);
fclose($pipe_back);
?>

 

EDIT: Didn't see the r+.  >_<  I changed my code a bit, so try it now.

Worked perfectly. Hum. Now I feel really silly. I only stuck it on r+ because th e first blog posting I read on the subject suggested that not doing so would break the pipe. Should've checked that..

 

*slaps forehead*

 

Thanks for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.