tags:

views:

157

answers:

1

I have just started to learn socket programming using Perl. Is there a method to send the output (STDOUT) data/images from already running scripts/tools using Perl socket programming?

+2  A: 

If your existing script dumps its output to the console (STDOUT) you can just redirect it using netcat (see nc(1)):

On the server:

server-host:~$ nc -l -p 3232 > file.txt

On the client:

client-host~$ perl -e 'printf( "Hello %s\n", "world" )' | nc server-host 3232

The choice if the port (3232) is of course arbitrary, but on Unix it should be greater then 1024 for you to be able to bind it without root privileges.

Edit:

How about this then:

#!/usr/bin/env perl
use strict;

my $usage = "Usage: $0 <host:port> <prog>\n";
my $hp = shift @ARGV or die "$usage";
my $prg = shift @ARGV or die "$usage";
my ($host,$port) = split ":", $hp;

defined $host or die "$usage";
defined $port or die "$usage";

exec "$prg @ARGV | nc $host $port" or die "can't execute $prg\n";
Nikolai N Fetissov
Thanks Nikolai: Can you also please give me some brief to automate this task.
Space
Thanks for this, but this is doing the same thing, after executing command the session breaks and for sending another command i have to again open the server.
Space
It's the `-k` option. *Read The Fine Manual* for kitty sake!!!
Nikolai N Fetissov