开发者

How can I send the output of external programs to a Perl socket?

I have just started to learn socket programming using Perl. Is there a method to send the output (STDOUT) data/images from already running script开发者_StackOverflow社区s/tools using Perl socket programming?


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜