Gossamer Forum
Home : General : Perl Programming :

Help on I/O streams in perl

Quote Reply
Help on I/O streams in perl
Hi,

I am trying to redirect STDOUT and STDERR from Linux to Windows using a socket in my perl script. The script is sending output to windows but it disturbs the sequence of output. If i first redirect STDOUT over socket and then do the same for STDERR then i received all prints coming from STDOUT first and prints coming from STDERR in the last, irrespective of print sequence in the script. This behavior is reversed if we reverse the redirection sequence, i.e. first redirect STDERR and then STDOUT.

Changing the sequence of following piece of code reverses the results, but in either case my requirement has not been met.

if (open (STDOUT, ">&SOCKET") != 1 || !open (STDERR, ">&STDOUT"))
{
print STDERR "\nError : Can not redirect\n";
exit -1;
}

I have also tried to redirect STDERR over SOCKET, but the results are same.

Below is complete script.

#!/usr/bin/perl -w
#use strict;
use Socket;
if ($#ARGV + 1 != 2)
{
print STDERR "\nError : Wrong number of arguments\n\tCan not proceed\n";
exit -1;
}

my $host = $ARGV[0];
my $port = $ARGV[1];
my $proto = getprotobyname('ip');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
socket(SOCKET, AF_INET, SOCK_STREAM, $proto) or die "socket: $!";
connect (SOCKET, $paddr) or die "connect: $!";
print STDOUT "\nRedirecting output\n";
close (STDOUT);
close (STDERR);
if (open(STDOUT,">&SOCKET") != 1 || !open(STDERR, ">&STDOUT")) {
print STDERR "Error: Cannot redirect STDERR to STDOUT","\n";
print STDOUT "\nSTDOUT is still without STDERR\n";
exit(-1);
}
print STDOUT "\nSTDOUT ---> This is test to redirect perl output\n";
print STDERR "\nSTDERR ---> STDERR redirected\n";
print STDERR "\nSTDERR ---> Check if sequence is correct\n";
print STDOUT "\nSTDOUT ---> Is test successful\n";
close (SOCKET) or die "close: $!";
exit 0;


Can any one help me to solve this problem?

Regards,

Azfar

Last edited by:

azfar_1: Dec 19, 2007, 6:58 AM