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

this may be a dumb question, but in any case here it is.

within the same machine, I'm using unix domain socket 
server/clients for communication. How can i get the
client script send back a result to the server script? here's my goal. i have 2 machines on the same network.
machine_a has multi users on it, and machine_b has only
a single user on it. in machine_b, I have a script that
generates some numbers. I'd like for the users on
machine_a to call a client.script that uses the socket
to contact my server.script on machine_a. Then my server.script will issue a "ssh me@machine_b /home/me/run_script". So far I've been able to do this. however, how would I
get my server.script to send back the results to
client.script so the users can know of the results
produced on machine_b? thanx for any input

Replies are listed 'Best First'.
Re: a dumb socket question
by beable (Friar) on Jul 14, 2004 at 23:38 UTC
    Hey, you didn't post any code! In general, to send a message on a socket, use the print Socket $msg function. You should read the perlipc documentation for lots of examples.
Re: a dumb socket question
by rupesh (Hermit) on Jul 15, 2004 at 05:32 UTC
Re: a dumb socket question
by castaway (Parson) on Jul 15, 2004 at 06:55 UTC
    Are you stuck on reading the results of the ssh call, or sending them to the client? You didn't say. Also, why does the server script call the run_script using ssh, if they are on the same machine? Surely you could skip that part and just call the run_script directly? (Using backticks or system, backticks will get you anything the script prints to STDOUT, eg: my $result = `run_script`;.

    To return it to the client, you just need to print that information to the socket.

    C.

      I'm stuck on sending the result back to the client.
      I think if I read the perlipc's doc as suggested by beable, it'll help
      but in any case here's the code:
      server.script: use Socket; ($Program = $0) =~ s@^.*/@@; $file = "/tmp/socket.$Program"; $addr = sockaddr_un($file); socket(SERVER, PF_UNIX, SOCK_STREAM, 0) || die "Could not create socke +t: $!"; unlink("$file"); bind(SERVER, $addr) || die "Could not bind: $!"; listen(SERVER,SOMAXCONN) || die "Could not listen: $!"; while (accept(CLIENT,SERVER)) { $cmd = <CLIENT>; if ($cmd) { $time = &timeStamp; open(CMD, "ssh2 rob\@machine_b $cmd |"); @results = <CMD>; close(CMD); print @results; } } client.script: use Socket; ($Program = $0) =~ s@^.*/@@; $file = "/tmp/server.script"; &Message("/home/me/run_script -r -s a1 -avg"); sub Message { my ($cmd) = @_; my $level = 1; if (socket(SOCKET, PF_UNIX, SOCK_STREAM, 0)) { connect(SOCKET, sockaddr_un($file)) || die "Could not connect: + $!"; print SOCKET $cmd; close SOCKET; } else { print "I couldn't connect"; } }
      OK, the client.script sends the query to server.script,
      and server.script runs the cmd & prints it. how do i get it
      to return the results to the client.script so the client.script
      can print the result? (I think i should go and read the perlipc's doc)
      thanx...
        How about writing the result to the CLIENT socket? Sockets are bidirectional, and the client script is already connected to the server. I don't see any synchronization issues.
        # server @results = <CMD>; print CLIENT @results; # client print SOCKET $cmd; @results = <SOCKET>;

        Why do you have a separate server and client? Since they are running on the same machine, wouldn't it be easier to just have the client script do the ssh command? The only reason I can think of is that the server script is running as a different user with ssh privledges.