Interprocess Communication using Named Pipes in C# + PHP
Asked Answered
C

2

5

Interprocess Communication using Named Pipes in C# is easy, but im not exactly sure how to do this in php, or if its even possible. so i have these questions:

  1. Is named pipes possible in php?
  2. Is it possible to have a C# named pipe client, connect to a php named pipe server?
  3. how the heck would i code that? :)

an answer to any of the above questions would be so helpful.. thanks :)

edit: Its a stand alone php program, not a web-based app.

edit2: The named pipe server can be in the C# side, or the PHP side, it doesnt matter. I have made C# examples for both.. but i dont know where to start for php

Chatman answered 2/9, 2010 at 1:18 Comment(0)
P
2

Can you use sockets? Why does it have to be a pipe?

Looks like PHP has lots for sockets: https://www.php.net/sockets

Stream Functions:
http://php.net/manual/en/ref.stream.php

Did you see this?
PHP and named pipes: http://my.opera.com/zomg/blog/2007/08/29/php-and-named-pipes

<?php
//Open pipe and write some text to it.
//Mode must be r+ or fopen will get stuck.
$pipe = fopen('testpipe','r+');
fwrite($pipe,'this is some text');
fclose($pipe);
?>

posix_mkfifo:
http://www.phpbuilder.com/manual/function.posix-mkfifo.php

EDIT I am assuming you are on windows (C#) so that may not work....

Plicate answered 2/9, 2010 at 1:21 Comment(4)
I suppose if named pipes doesnt work, then that is a possibility.. lets call that plan B :)Chatman
@Tommy: why do you have to steal my name? hahaPlicate
no, im on windows. and, i need the pipe to be multi-directional. so, the php needs to open the pipe, be able to write to the pipe, and also wait for input and read anything else sent from the pipe server.Chatman
And, what about reading after writing ?Rubbing
E
4

If it is already created then you can open a named pipe as a file using PHP's fopen function.

In windows the pipe "file" path looks like "\\.\pipe\pipe_name", however there is an open issue in PHP which prevents this from working. The workaround is to use the computer's name instead of the dot in the path:

$name = php_uname('n');
$pipe = fopen("\\\\" . $strComputername . "\\pipe\\pipe_name", "r+");

Though I vote for sockets like Tommy recommended, they're easy, cross-platform, and inter-machine if need be.

Excurvature answered 2/9, 2010 at 1:31 Comment(1)
And what about for writing, and for reading ?Rubbing
P
2

Can you use sockets? Why does it have to be a pipe?

Looks like PHP has lots for sockets: https://www.php.net/sockets

Stream Functions:
http://php.net/manual/en/ref.stream.php

Did you see this?
PHP and named pipes: http://my.opera.com/zomg/blog/2007/08/29/php-and-named-pipes

<?php
//Open pipe and write some text to it.
//Mode must be r+ or fopen will get stuck.
$pipe = fopen('testpipe','r+');
fwrite($pipe,'this is some text');
fclose($pipe);
?>

posix_mkfifo:
http://www.phpbuilder.com/manual/function.posix-mkfifo.php

EDIT I am assuming you are on windows (C#) so that may not work....

Plicate answered 2/9, 2010 at 1:21 Comment(4)
I suppose if named pipes doesnt work, then that is a possibility.. lets call that plan B :)Chatman
@Tommy: why do you have to steal my name? hahaPlicate
no, im on windows. and, i need the pipe to be multi-directional. so, the php needs to open the pipe, be able to write to the pipe, and also wait for input and read anything else sent from the pipe server.Chatman
And, what about reading after writing ?Rubbing

© 2022 - 2024 — McMap. All rights reserved.