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

Completely new area for me, so I need some initial ideas from fellow monks. I have the following scenario: a) client 1 sends data via GET to CGI script on server. When CGI script receives data, it b) manipulates it and c) it needs to send the result to client 2.

I have already implemented a) and b), but have no idea how can I send data to second client. Data are small chunks of text.

First naive idea that came into mind: CGI writes data to file, client 2 reads data from file. However, this is not a one shot operation, but this is almost a continuous loop client1->server->client2, so I think my idea is too naive even for me. What (easy) approach would you recommend? What should I look for? Any suggestion very much appreciated.

Replies are listed 'Best First'.
Re: CGI send data to client
by bliako (Abbot) on Dec 07, 2018 at 18:19 UTC

    In addition to communicating via a file (both CGI and client2 are on the same machine is what I understood, also that client2 is running as a daemon waiting for data), you can also have them communicating via a socket, its port must be blocked by firewall so that client2 is not accessed externally.

    Every time client1 calls the CGI script, a new instance of the script is created. Which means that a new socket to client2 must be created every time. So, this solution too has its overheads.

    Assuming you do not have too many requests, then writing data to a file may not be a bad solution. It could be faster if you created a ramdisk and write/read all your data files there.

    If there are multiple parallel requests from client1 to server then you will have a problem with unique datafiles/requests to client2 and also keeping track of whose request was first so that it is served first. Both problems easily sorted but with added overheads.

    The most controversial solution would be for the server to system() client2 with all of its data either as a commandline param or as a file.

    If you are flexible and client2 is not already a standalone program, why not write client2 as a Perl module and have your CGI script do something like:

    # in CGI script use Client2; my $client2 = new Client2($params); my $output = $client2->process($data); print $output;

    That's the route I would take.

    bw, bliako

Re: CGI send data to client
by NetWallah (Canon) on Dec 07, 2018 at 22:31 UTC
        >>send the result to client 2

    It is not clear HOW you want data sent to client2.

    If client2 runs a we browser, and expects data via CGI, then we are talking about a "chat" type application, and you need to consider ajax type async communication.

    If client2 accepts data via a socket (possibly UDP), then the CGI script could potentially just dump it there.

    Then , there are FIFO or memory-file or message-queue type options for other situations.

    A little more clarity could get you better guidance.

                    Memory fault   --   brain fried

      Here are some other infos: client1 is on 1st machine and client2 on a second machine, both connected through the Web with the server. client2 is just a Perl script that needs to get the data, process it and display results in a simple Tk window. Yesterday I implemented my naive solution:

      • server writes in file what it receives from client1
      • client2 reads every x-time file from server by means of GET

      Of course it works, but even if I am at a beginner in client-server solutions, I know this is not the way to do it. I'll start having a look at "ajax type async communication" and what can I do in Perl.