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
2010-04-13 13:13:46
Thanks Nikolai: Can you also please give me some brief to automate this task.
Space
2010-04-14 06:09:40
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
2010-04-15 05:11:39
It's the `-k` option. *Read The Fine Manual* for kitty sake!!!
Nikolai N Fetissov
2010-04-16 04:13:26