简体   繁体   中英

PHP webserver connect to C# program on same server — What am I looking for?

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.

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 :-)

  • If written with care, you can also abstract the service to handle multiple devices at the same time

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");

XML-RPCSOAP设计用于应用程序之间的通信。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM