I'm trying to send some data from php staight into a running cpp script. I'm trying to do this using a named pipe in a "send" application. Let me share some code first.
loop.cpp
main( int argc, char* argv[] ){
//open pipe
int action;
int value;
char action_result[ BUFSIZ ];
char value_result[ BUFSIZ ];
mkfifo( FIFO_ACTION, 0666 );
mkfifo( FIFO_VALUE, 0666 );
//loop variables
bool close_loop = false;
unsigned int i = 0;
while( !close_loop ){
action = open( FIFO_ACTION, O_RDONLY );
value = open( FIFO_VALUE, O_RDONLY );
read( action, action_result, BUFSIZ );
read( value, value_result, BUFSIZ );
if( strcmp( "", action_result ) != 0 ){
if( strcmp( "ly", action_result ) == 0 ){
cout << "Recieved: " << value_result << endl;
} else if( strcmp( "cl", action_result ) == 0 ){
break;
}
}
memset( action_result, 0, sizeof(action_result) );
memset( value_result, 0, sizeof(value_result) );
close( value );
close( action );
usleep( 5000 );
}
close( action );
close( value );
unlink( FIFO_ACTION );
unlink( FIFO_VALUE );
cout << "Closing output" << endl;
return 0;
}
send.cpp
int main( int argc, char* argv[] ){
int action;
int value;
/* write str to the FIFO */
action = open( FIFO_ACTION, O_WRONLY );
value = open( FIFO_VALUE, O_WRONLY );
write( value, argv[ 1 ], sizeof( argv[ 1 ] ) );
write( action, "ly", sizeof( "ly" ) );
cout << "Sended: " << argv[ 1 ] << endl;
close( value );
close( action );
/* remove the FIFO */
return 0;
}
If I run both apps from the terminal, everything works fine and I get my output. But I have to use the following code to execute the send.cpp file from php.
exec( "/home/pi/Bureaublad/send $id", $output );
print_r($output);
When I do this, I get the correct result "Sended: 14" as in the terminal, however, the pipes seem not to work as I don't have any output in my loop.cpp running from the terminal.
I personaly suspect there is some unix user stuff going on but I can't find anything about it using google. Whatever I try, I don't get an output.
Can somebody please point me in the right direction?
Thanks a thousand times!!