简体   繁体   English

使用Perl和C#中的套接字在本地计算机上进行进程间通信

[英]inter-process communication on local machine using socket in perl and c#

I am working on ac# application that spawn new Processes to run Perl programs: 我正在使用ac#应用程序,该应用程序会生成新的进程来运行Perl程序:

I was wondering if there is a way to use socket interface to let perl program to talk to c# application. 我想知道是否有一种使用套接字接口的方法让perl程序与c#应用程序通信。 If using socket, the address has to be local host: 127.0.0.1? 如果使用套接字,则该地址必须是本地主机:127.0.0.1? How to choose which port number to use? 如何选择要使用的端口号?

also, 也,

Since the C# application spawn a Process to run Perl program, is there a way to use inter-process communication in c# to achieve this task? 由于C#应用程序产生了一个进程来运行Perl程序,是否有一种方法可以在c#中使用进程间通信来完成此任务? I mean maybe the process that is running the perl can send a message to the c# appilication? 我的意思是也许正在运行perl的进程可以向c#应用程序发送消息?

Thanks. 谢谢。

Use the IO::Socket::INET module. 使用IO :: Socket :: INET模块。

You can connect to a port on localhost 您可以连接到本地主机上的端口

$sock = IO::Socket::INET->new('127.0.0.1:2525');

or to another address 或到另一个地址

$sock = IO::Socket::INET->new("host.example.com:6789");

These examples assume the Perl program will be the client and you've written the server in C#. 这些示例假定Perl程序将是客户端,并且您已经用C#编写了服务器。 If it's the other way around, use the IO::Select module. 如果不是这样,请使用IO :: Select模块。 Below is an example from its documentation: 以下是其文档中的示例:

use IO::Select;
use IO::Socket;

$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
$sel = new IO::Select( $lsn );

while (@ready = $sel->can_read) {
    foreach $fh (@ready) {
        if ($fh == $lsn) {
            # Create a new socket
            $new = $lsn->accept;
            $sel->add($new);
        }
        else {
            # Process socket
            # Maybe we have finished with the socket
            $sel->remove($fh);
            $fh->close;
        }
    }
}

Using this code, you'd then connect from C# to port 8080 on the localhost. 然后使用此代码从C#连接到本地主机上的端口8080。

The choice of port is mostly arbitrary. 端口的选择大部分是任意的。 Both sides need to agree on the rendezvous port, and you want to avoid ports below 1024. Whether you connect to localhost or another address is determined by the address to which the server is bound. 双方都需要在集合点端口上达成共识,并且要避免端口低于1024。连接到localhost还是其他地址由服务器绑定的地址确定。 To bind to a network-accessible address, modify the above code to use 要绑定到网络可访问的地址,请修改以上代码以使用

$lsn = new IO::Socket::INET(Listen => 1, LocalAddr => "host.example.com:8080");

The backlog of size 1 (the Listen parameter) is unusual. 大小为1(“ Listen参数)的积压异常。 A typical size is the value of SOMAXCONN from sys/socket.h . 一个典型的大小是sys/socket.hSOMAXCONN的值。

您可以尝试命名管道(.NET侧为System.IO.Pipes,Perl侧为Win32 :: Pipe)。

Your best option is a socket. 最好的选择是插座。 You can choose any port that is not in use, and is above 1024. But you might want to review a list of common port assignments just to make sure you don't choose a conflict with a program you have in your environment. 您可以选择任何未使用且高于1024的端口。但是您可能想要查看常用端口分配列表,以确保您不会选择与环境中的程序冲突的端口

-- Edit: -编辑:

It seems that link advises port numbers above 49152. Wow, times change :) 似乎该链接建议使用高于49152的端口号。哇,时间在变:)

您可以在没有套接字和管道的情况下进行管理:在C#中,重定向生成的进程的标准输入和输出( 例如 ),然后编写perl脚本,该脚本从STDIN接收数据并将结果发送到STDOUT。

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

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