hi all,

I am having a problem with duping stdout and socket. When my client forks of children, 
the childrens stdout is then duped with a socket. Which is what I want, but for some 
reason the parents stdout gets redirected as well. I want the parents STDOUT to stay 
the same and only redirect childs stdout. Am I missing something or forgeting 
something ?? 

thanx,
Mark


_____paste server_____

use Socket qw(:DEFAULT :crlf);
use IO::Handle;
use Fcntl;
use POSIX;

my $PORT = 2100;
$|=1;

#get socket and make it reusuable,set it to raw sock 
socket($SOCKET,AF_INET,SOCK_DGRAM,scalar getprotobyname('udp') ) || myerrno();

$my_addr=sockaddr_in($PORT,INADDR_ANY);
bind($SOCKET,$my_addr);

$SOCKET->blocking(0);
$SOCKET->autoflush(1);


 
print "server up ad ready for connections\n";
# start recieving the data

while(){

     $remote_addr=recv($SOCKET,$buff,"1000",0) or die "problem with recv: $!";

     $msg_recived++;

     ($R_port,$R_addr)=sockaddr_in($remote_addr);
           $R_addr=inet_ntoa($R_addr);

     print "\nrecived:\n $buff\n...from $R_addr:$R_port MSG number $msg_recived\n";
}

sub myerrno{
 foreach $Val (keys( %!) ){
  print "$Val $!{$Val}\n" if $!{$Val}; 
 
 }
exit(1);
}

_____paste server_____



_____cut client_______

use POSIX qw ( :sys_wait_h :unistd_h);
use Socket qw(:DEFAULT :crlf);
use IO::Handle;
use Fcntl;



my $CHILD_SPAWNED=0;
my $CHILD_TO_CREATE=10;
my $MAX_CHILD=3;
my $PORT = 2100;
my $CHILD_CREATED=0;
$|=1;





while( $CHILD_SPAWNED < $CHILD_TO_CREATE ){

# lets get the socket readY


#get socket  
socket($SOCKET_OUT,AF_INET,SOCK_DGRAM,scalar getprotobyname('udp') ) || myerrno();

# make it non-buffred non-block IO
$SOCKET_OUT->blocking(0);
$SOCKET_OUT->autoflush(1);

 
# pack the destination address with port
$f_addr=sockaddr_in($PORT,inet_aton("127.0.0.1") );

# set the destination address on socket
connect($SOCKET_OUT,$f_addr) || myerrno();




if( $CHILD_CREATED < $MAX_CHILD ){

 die "couldnt not spawn: $!" unless defined ( my $PID=fork() );

 $CHILD_CREATED++; 
 $CHILD_SPAWNED++;

 unless( $PID ){

$status=dup2(fileno($SOCKET_OUT),fileno(STDOUT) );
print $SOCKET_OUT "hello from $$\n";


 exit(0);
 }


}


 close $SOCKET_OUT;

$status=waitpid(-1,WNOHANG);

if( $status != 0 ){ #some child has exited
 $CHILD_CREATED--;

 print "child $status has exited \n";
}




}


syswrite(\*STDOUT, "TOTAL CHILD SPAWNED IS $CHILD_SPAWNED\n");

sub myerrno{
 foreach $Val (keys( %!) ){
  print "$Val $!{$Val}\n" if $!{$Val}; 
 
 }
exit(1);
}

____paste client____

Reply via email to