tags:

views:

621

answers:

4

So I have a webserver in which user can remotely control an external electronic board which due to certain conditions force me to put a desktop program using C# as the middle-man.

What is the magic keyword I'm looking for? At first I thought of socket but every socket searches involve server-client over TCP... it's the same machine so theoretically I can just put loopback address and proceed normally. But is this an overkill way or the only way?

Thanks in advance.

edit: My C# program is basically a daemon which will wait orders from the PHP script. So I can remotely access that website and instruct that C# app.

+1  A: 

I'm not 100% sure what you mean. I assume you want to call a C# app from within a php script. If so, maybe this will help:

function execute($command, $stdin) {
 $pipes = array();
 $process = proc_open($command, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes);
 if ($stdin) fwrite($pipes[0], $stdin);
 fclose($pipes[0]);
 $stdout = '';
 while(!feof($pipes[1])) $stdout .= fgets($pipes[1], 1024);
 fclose($pipes[1]);
 $stderr = '';
 while(!feof($pipes[2])) $stderr .= fgets($pipes[2], 1024);
 fclose($pipes[2]);
 $return_value = proc_close($process);
 return array($stdout, $stderr, $return_value);
}

Where $command is the path and file name of your c# app (plus any command line params) and if needed $stdin is, well, standard input to your c# app.

If your C# app is listening on a particular port (lets say 8888) for requests, maybe you're looking for:

$handle = fopen("http://localhost:8888/someurl?someparam=somevalue", "r");
Stacey Richards
Note that this references using a console app, which may well be a better method.
C. Ross
No, I want to pass a message to the C# app that should always be running. Read my edit.
SyaZ
+2  A: 

I would suggest client/server architecture using TCP/IP (or even UDP) (or another MS messaging protocol) in order to talk to your C# program. What you are effectively doing is writing a device driver for a specialized piece of hardware. By making it client/server you can:

  • Shift the code into a Windows service which can run on any machine your web server can connect to, not just the local machine.
  • Easily have the server handle multiple connections, so that clients other than your web server can access the hardware and the access contention is handled in a rational manner. Think test console for debugging/testing the hardware.
  • Add a cool line to your resume about writing client/server systems and windows services!
  • Probably some other benefits that I can't think of now :-)

Edit

  • If written with care, you can also abstract the service to handle multiple devices at the same time
Peter M
+1  A: 

XML-RPC and SOAP are designed for application-to-application communication.

raspi
A: 

Hi, i have to do the same thing. I have a webserver with PHP user interface, and with buttons, i will like that PHP sends orders to a C# program in the same machine. SyaZ, You fix it?

(I dont speak english very well).