duc has asked for the wisdom of the Perl Monks concerning the following question:

Hi !

This is just a general question about communication between programs. Yeah.. I need to do this a lot.

I was wondering if I could use a socket to communicate between two programs (both ways) if one program is in perl and the other is a console application (which I can edit). I know the two programs should "talk" and "listen" to the same socket. And by the way if I am under Windows what address family do I give to these sockets ? Oh, and the two programs are likely to be on the same computer.

I was also considering to transform my console application into a dll and call the dll function in perl. Is it possible with the standard modules ? I know there is Win32::API but I don't have it and I can't get to install it on my machine. I would also have to send arrays (5 in one of my application) to the function and return an array. Would that cause a problem ?

I've been trying to use IPC::Open2 but it does not work, so I am looking for some other solutions !

Replies are listed 'Best First'.
Re: using socket with c++
by liverpole (Monsignor) on Jul 26, 2006 at 13:33 UTC
    Hi duc,

        I was wondering if I could use a socket to communicate between two programs (both ways) if one program is in perl and the other is a console application ...

    Absolutely!  The whole point of socket programming is that it doesn't matter what is at either end of the socket, as long as they are both 'speaking the same language' (using the correct protocol).  And if you are doing the programming at both ends, you get to define the protocol.

    I don't know as much about Windows programming, so I'm afraid I can't help with your Windows specific questions.  But if you take a look at the perl modules IO::Select and IO::Socket, they should help you to do everything you need to from the Perl side.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: using socket with c++
by cdarke (Prior) on Jul 26, 2006 at 16:08 UTC
    Sockets will work perfectly OK, use AF_INET, and stick with WinSock 1. AF_UNIX is defined in winsock.h, but does not work (no surprise!).
    Calling a dll may require a small XS wrapper. Make sure your C++ entry point uses __stdcall (or WINAPI) calling convention with __declspec dllexport, otherwise you will get name mangling and parameters will not be passed correctly.
    C++ arrays and Perl arrays are not directly compatible, but it is not difficult to write XS to do this.
Re: using socket with c++
by sgifford (Prior) on Jul 26, 2006 at 16:18 UTC

      Thanks, I will have a look at this module.

      As for XS, I can't get through the first example of the tutorial, 'make' is failing :(. But I still think it could be handy. :)

        Try nmake
Re: using socket with c++
by Moron (Curate) on Jul 27, 2006 at 12:10 UTC
    IPC::Open2 doesn't work because the process has to be restarted each time it is called. Before jumping into sockets, it might be easier to use is a Win32::Pipe (i.e. a Win32 named pipe)

    -M

    Free your mind