简体   繁体   English

PHP Web服务器连接到同一服务器上的C#程序-我在寻找什么?

[英]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. 因此,我有一个Web服务器,用户可以在其中远程控制外部电子板,由于某些条件,该电路板迫使我使用C#作为中间人来放置桌面程序。

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. 起初,我想到了套接字,但是每个套接字搜索都涉及TCP上的服务器-客户端...这是同一台机器,因此从理论上讲,我可以放回送地址并正常进行。 But is this an overkill way or the only way? 但这是一种矫kill过正的方法还是唯一的方法?

Thanks in advance. 提前致谢。

edit: My C# program is basically a daemon which will wait orders from the PHP script. 编辑:我的C#程序基本上是一个守护程序,它将等待来自PHP脚本的命令。 So I can remotely access that website and instruct that C# app. 因此,我可以远程访问该网站并指示该C#应用程序。

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. 我建议使用TCP / IP(甚至UDP)(或其他MS消息传递协议)的客户端/服务器体系结构,以便与您的C#程序对话。 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. 将代码转移到Windows服务中,该服务可以在您的Web服务器可以连接到的任何计算机上运行,​​而不仅仅是本地计算机。
  • 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. 轻松地让服务器处理多个连接,以便Web服务器以外的其他客户端都可以访问硬件,并且以合理的方式处理访问争用。 Think test console for debugging/testing the hardware. 考虑用于调试/测试硬件的测试控制台。
  • Add a cool line to your resume about writing client/server systems and windows services! 在您的简历中添加一条关于编写客户端/服务器系统和Windows服务的妙语!
  • 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 如果编写得当,您还可以抽象化服务以同时处理多个设备

I'm not 100% sure what you mean. 我不确定100%的意思。 I assume you want to call a C# app from within a php script. 我假设您想从php脚本中调用C#应用程序。 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. 其中$ command是c#应用程序的路径和文件名(加上任何命令行参数),如果需要,$ stdin是c#应用程序的标准输入。

If your C# app is listening on a particular port (lets say 8888) for requests, maybe you're looking for: 如果您的C#应用​​正在特定端口(例如8888)上侦听请求,那么您可能正在寻找:

$handle = fopen("http://localhost:8888/someurl?someparam=somevalue", "r");

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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